FoxSemanticChecker.Mod 402 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  128. BEGIN
  129. IF diagnostics # NIL THEN
  130. Basic.GetErrorMessage(code,message,errorMessage);
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  134. END;
  135. error := TRUE;
  136. END Error;
  137. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  138. VAR errModule: SyntaxTree.Module;
  139. BEGIN
  140. IF diagnostics # NIL THEN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  143. END;
  144. END Warning;
  145. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  146. VAR errorMessage: ARRAY 256 OF CHAR;
  147. BEGIN
  148. IF diagnostics # NIL THEN
  149. Basic.Concat(errorMessage,msg," ", msg2);
  150. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  151. END;
  152. error := TRUE;
  153. END ErrorSS;
  154. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  155. VAR msg, msg2: ARRAY 256 OF CHAR;
  156. BEGIN
  157. IF diagnostics # NIL THEN
  158. COPY(msg1, msg);
  159. Strings.Append(msg, " = ");
  160. Basic.GetString(s, msg2);
  161. Strings.Append(msg, msg2);
  162. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  163. END;
  164. END InfoSS;
  165. (*** symbol lookup ***)
  166. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  167. **)
  168. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  169. VAR
  170. scope,baseScope: SyntaxTree.Scope;
  171. symbol, s: SyntaxTree.Symbol;
  172. ownerRecord,base: SyntaxTree.RecordType;
  173. BEGIN
  174. scope := inScope;
  175. symbol := NIL;
  176. WHILE (scope # NIL) & (symbol = NIL) DO
  177. symbol := scope.FindSymbol(name);
  178. s := NIL;
  179. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  180. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  181. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  182. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  183. END;
  184. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  185. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  186. ELSE
  187. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  188. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  189. symbol.MarkUsed;
  190. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  191. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  192. base := RecordBase(ownerRecord);
  193. IF (base # NIL) THEN
  194. baseScope := base.recordScope;
  195. symbol := Find(baseScope,name,FALSE);
  196. ELSE
  197. symbol := NIL;
  198. END;
  199. ELSE
  200. symbol := NIL;
  201. END;
  202. END;
  203. END;
  204. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  205. END;
  206. IF (symbol # NIL) THEN
  207. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  208. ASSERT(phase = DeclarationPhase);
  209. ResolveSymbol(symbol)
  210. END;
  211. symbol.MarkUsed;
  212. END;
  213. RETURN symbol
  214. END Find;
  215. (*** types ***)
  216. (** find type declaration with name qualifiedIdentifier and return resolved type
  217. - check qualified identifier prefix, set scope to module scope if appropriate
  218. - check suffix in scope
  219. **)
  220. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  221. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  222. BEGIN
  223. result := NIL;
  224. prevScope := currentScope;
  225. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  226. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  227. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  228. IF symbol(SyntaxTree.Import).module = NIL THEN
  229. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  230. result := SyntaxTree.invalidType;
  231. symbol := NIL;
  232. ELSE
  233. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  234. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  235. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  236. IF VerboseErrorMessage THEN
  237. Printout.Info("scope", currentScope);
  238. Printout.Info("symbol", symbol);
  239. END;
  240. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  241. END;
  242. END;
  243. ELSE
  244. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  245. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  246. symbol := NIL;
  247. END;
  248. ELSE
  249. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  250. IF symbol = NIL THEN
  251. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  252. IF VerboseErrorMessage THEN
  253. Printout.Info("Qualident",qualifiedIdentifier);
  254. Printout.Info("in scope",currentScope) ;
  255. END;
  256. END;
  257. END;
  258. IF symbol = NIL THEN (* error already handled *)
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  262. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  263. typeDeclaration := NIL;
  264. result := SyntaxTree.invalidType;
  265. ELSE
  266. currentScope := symbol.scope;
  267. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  268. result := ResolveType(typeDeclaration.declaredType);
  269. symbol.MarkUsed;
  270. ASSERT(result # NIL);
  271. END;
  272. currentScope := prevScope;
  273. RETURN result
  274. END ResolveNamedType;
  275. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  276. If node is currently being resolved then emit a cyclic definition error.
  277. Return TRUE only if node is fully resolved.
  278. **)
  279. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  280. VAR result: BOOLEAN;
  281. BEGIN
  282. IF SyntaxTree.Resolved IN x.state THEN
  283. result := FALSE
  284. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  285. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  286. result := FALSE;
  287. ELSE
  288. result := TRUE;
  289. x.SetState(SyntaxTree.BeingResolved)
  290. END;
  291. RETURN result
  292. END TypeNeedsResolution;
  293. (** Return invalid type if x is currently being resolved, return x otherwise**)
  294. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  295. BEGIN
  296. IF SyntaxTree.Resolved IN x.state THEN
  297. RETURN x
  298. ELSE
  299. RETURN SyntaxTree.invalidType
  300. END;
  301. END ResolvedType;
  302. PROCEDURE VisitType(x: SyntaxTree.Type);
  303. BEGIN
  304. ASSERT(x = SyntaxTree.invalidType);
  305. END VisitType;
  306. (** resolve basic type **)
  307. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  308. BEGIN
  309. IF TypeNeedsResolution(x) THEN
  310. x.SetState(SyntaxTree.Resolved);
  311. END;
  312. resolvedType := ResolvedType(x)
  313. END VisitBasicType;
  314. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitByteType;
  318. (** resolve character type **)
  319. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitCharacterType;
  323. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitBooleanType;
  327. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitSetType;
  331. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitAddressType;
  335. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitSizeType;
  339. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitAnyType;
  343. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitObjectType;
  347. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitNilType;
  351. (** resolve integer type **)
  352. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitIntegerType;
  356. (** resolve real type **)
  357. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitFloatType;
  361. (** resolve complex type **)
  362. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  363. BEGIN
  364. VisitBasicType(x);
  365. END VisitComplexType;
  366. (**
  367. resolve string type: nothing to be done
  368. **)
  369. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  370. BEGIN
  371. IF TypeNeedsResolution(x) THEN
  372. x.SetState(SyntaxTree.Resolved);
  373. END;
  374. resolvedType := ResolvedType(x)
  375. END VisitStringType;
  376. (**
  377. check enumeration scope: enter symbols and check for duplicate names
  378. **)
  379. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  380. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  381. BEGIN
  382. prevScope := currentScope;
  383. currentScope := x;
  384. e := x.firstConstant;
  385. WHILE (e # NIL) DO
  386. Register(e,x,FALSE);
  387. IF SymbolNeedsResolution(e) THEN
  388. IF e.value # NIL THEN
  389. value := ConstantExpression(e.value);
  390. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  391. ELSE
  392. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  393. value.SetType(x.ownerEnumeration);
  394. END;
  395. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  396. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  397. IF nextHighest > highest THEN highest := nextHighest END;
  398. END;
  399. e.SetValue(value);
  400. CheckSymbolVisibility(e);
  401. e.SetType(x.ownerEnumeration);
  402. e.SetState(SyntaxTree.Resolved);
  403. END;
  404. e := e.nextConstant;
  405. END;
  406. currentScope := prevScope;
  407. END CheckEnumerationScope;
  408. (**
  409. resolve enumeration type: check enumeration scope
  410. **)
  411. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  412. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  413. lowest, highest: LONGINT;
  414. BEGIN
  415. IF TypeNeedsResolution(x) THEN
  416. IF x.enumerationBase # NIL THEN
  417. position := x.enumerationBase.position;
  418. baseType := ResolveType(x.enumerationBase);
  419. resolved := baseType.resolved;
  420. baseScope := NIL;
  421. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  422. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  423. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  424. ELSE
  425. enumerationBase := resolved(SyntaxTree.EnumerationType);
  426. lowest := enumerationBase.rangeHighest+1;
  427. END;
  428. x.SetEnumerationBase(baseType);
  429. ELSE lowest := 0;
  430. END;
  431. highest := lowest-1;
  432. CheckEnumerationScope(x.enumerationScope, highest);
  433. x.SetRange(lowest, highest);
  434. x.SetState(SyntaxTree.Resolved);
  435. END;
  436. resolvedType := ResolvedType(x);
  437. END VisitEnumerationType;
  438. (**
  439. resolve range type: nothing to be done
  440. **)
  441. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  442. BEGIN
  443. IF TypeNeedsResolution(x) THEN
  444. x.SetState(SyntaxTree.Resolved);
  445. END;
  446. resolvedType := ResolvedType(x)
  447. END VisitRangeType;
  448. (**
  449. resolve qualified type
  450. - find and resolve named type and set resolved type
  451. **)
  452. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  453. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  454. BEGIN
  455. IF TypeNeedsResolution(x) THEN
  456. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  457. x.SetResolved(type.resolved);
  458. x.SetState(SyntaxTree.Resolved);
  459. x.SetTypeDeclaration (typeDeclaration);
  460. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  461. x.SetResolved(SyntaxTree.invalidType);
  462. END;
  463. resolvedType := x;
  464. END VisitQualifiedType;
  465. (**
  466. resolve array type
  467. - check base type
  468. - array of math array forbidden
  469. - static array of open array forbidden
  470. **)
  471. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  472. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  473. BEGIN
  474. IF TypeNeedsResolution(x) THEN
  475. x.SetArrayBase(ResolveType(x.arrayBase));
  476. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  477. arrayBase := x.arrayBase.resolved;
  478. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  479. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  480. pointerType.SetPointerBase(arrayBase);
  481. pointerType.SetHidden(TRUE);
  482. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  483. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  484. ELSE
  485. x.SetArrayBase(pointerType);
  486. END;
  487. END;
  488. IF x.length # NIL THEN
  489. variableAccessed := FALSE;
  490. e := ResolveExpression(x.length);
  491. IF (e.resolved = NIL) THEN
  492. IF variableAccessed THEN
  493. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  494. END;
  495. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  496. ELSE
  497. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  498. END;
  499. END;
  500. IF arrayBase IS SyntaxTree.ArrayType THEN
  501. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  502. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  503. END;
  504. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  505. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  506. END;
  507. x.SetHasPointers(arrayBase.hasPointers);
  508. x.SetState(SyntaxTree.Resolved);
  509. END;
  510. resolvedType := ResolvedType(x);
  511. END VisitArrayType;
  512. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  513. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  514. BEGIN
  515. module := currentScope.ownerModule;
  516. IF module.name=name THEN
  517. (* do nothing *)
  518. ELSE
  519. moduleScope := module.moduleScope;
  520. import := moduleScope.FindImport(name);
  521. IF import = NIL THEN
  522. import := SyntaxTree.NewImport(position,name,name,TRUE);
  523. moduleScope.AddImport(import);
  524. Register(import,moduleScope,FALSE);
  525. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  526. VisitImport(import);
  527. ELSIF import.direct=FALSE THEN
  528. import.SetScope(module.moduleScope);
  529. import.SetDirect(TRUE);
  530. IF moduleScope.FindSymbol(import.name) = NIL THEN
  531. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  532. duplicate.SetContext(import.context);
  533. duplicate.SetModule(import.module);
  534. Register(duplicate,moduleScope,TRUE);
  535. VisitImport(duplicate);
  536. END;
  537. END;
  538. import.MarkUsed
  539. END;
  540. END ImportModule;
  541. (**
  542. resolve math array type
  543. - check base type
  544. - open math array of array forbidden
  545. - math array of tensor forbidden
  546. - static array of open array forbidden
  547. **)
  548. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  549. VAR arrayBase: SyntaxTree.Type;
  550. BEGIN
  551. IF TypeNeedsResolution(x) THEN
  552. x.SetArrayBase(ResolveType(x.arrayBase));
  553. IF x.length # NIL THEN
  554. x.SetLength(ConstantIntegerGeq0(x.length));
  555. END;
  556. arrayBase := x.arrayBase;
  557. IF arrayBase # NIL THEN
  558. arrayBase := arrayBase.resolved;
  559. IF arrayBase = SyntaxTree.invalidType THEN
  560. (* error already handled *)
  561. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  562. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  563. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  564. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  565. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  566. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  567. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  568. END;
  569. END;
  570. IF x.form = SyntaxTree.Static THEN
  571. x.SetIncrement(system.SizeOf(arrayBase));
  572. END;
  573. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  574. END;
  575. x.SetState(SyntaxTree.Resolved);
  576. END;
  577. resolvedType := ResolvedType(x);
  578. END VisitMathArrayType;
  579. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  580. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  581. (1) Rec = RECORD ... END; Ptr <---> Rec
  582. Ptr = POINTER TO Rec; ^ |
  583. | |
  584. TypeDesc TypeDesc
  585. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  586. ^ /
  587. | /
  588. TypeDesc <-- /
  589. *)
  590. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  591. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  592. BEGIN
  593. Strings.IntToStr(x.position,number);
  594. COPY(prefix,name);
  595. Strings.Append(name,"@");
  596. Strings.Append(name,number);
  597. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  598. typeDeclaration.SetDeclaredType(x);
  599. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  600. x.SetTypeDeclaration(typeDeclaration);
  601. currentScope.AddTypeDeclaration(typeDeclaration);
  602. typeDeclaration.SetScope(currentScope);
  603. END AnonymousTypeDeclaration;
  604. (**
  605. deferred pointer type resolving
  606. - resolve base type
  607. - check that base type is a record or array type
  608. - if error then set base type to invalid type
  609. **)
  610. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  611. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  612. BEGIN
  613. ASSERT(type.pointerBase # NIL);
  614. position := type.pointerBase.position;
  615. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  616. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  617. (* not for pointers, a type is needed for the records only
  618. IF type.typeDeclaration = NIL THEN
  619. AnonymousTypeDeclaration(type);
  620. END;
  621. *)
  622. END;
  623. resolved := ResolveType(type.pointerBase);
  624. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  625. type.SetPointerBase(resolved);
  626. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  627. recordType := resolved.resolved(SyntaxTree.RecordType);
  628. IF recordType.isObject & (recordType.baseType # NIL) THEN
  629. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  630. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  631. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  632. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  633. END;
  634. END;
  635. END;
  636. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  637. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  638. END;
  639. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  640. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  641. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  642. ELS
  643. *)
  644. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  645. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  646. END;
  647. END;
  648. ELSE
  649. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  650. type.SetPointerBase(SyntaxTree.invalidType)
  651. END
  652. END FixPointerType;
  653. (**
  654. resolve pointer type
  655. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  656. **)
  657. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  658. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  659. modifiers: SyntaxTree.Modifier; position: LONGINT;
  660. BEGIN
  661. IF TypeNeedsResolution(x) THEN
  662. modifiers := x.modifiers;
  663. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  664. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  665. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  666. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  667. (* inheritance cycle check
  668. example:
  669. A=POINTER TO RECORD(B) END;
  670. B=POINTER TO RECORD(A) END;
  671. *)
  672. IF x.pointerBase IS SyntaxTree.RecordType THEN
  673. recordType := x.pointerBase(SyntaxTree.RecordType);
  674. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  675. recordBaseType := ResolveType(recordType.baseType);
  676. recordType.SetBaseType(recordBaseType);
  677. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  678. END;
  679. CheckModifiers(modifiers, TRUE);
  680. typeFixes.Add(x,currentScope);
  681. x.SetState(SyntaxTree.Resolved);
  682. END;
  683. resolvedType := ResolvedType(x)
  684. END VisitPointerType;
  685. (**
  686. resolve port type
  687. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  688. **)
  689. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  690. VAR value: LONGINT;
  691. BEGIN
  692. IF TypeNeedsResolution(x) THEN
  693. x.SetCellsAreObjects(cellsAreObjects);
  694. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  695. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  696. x.SetSize(value)
  697. ELSE
  698. x.SetSize(system.SizeOf(system.longintType));
  699. END;
  700. x.SetState(SyntaxTree.Resolved);
  701. END;
  702. resolvedType := ResolvedType(x)
  703. END VisitPortType;
  704. (**
  705. deferred procedure type resolving
  706. - resolve return type
  707. - traverse and resolve parameters
  708. **)
  709. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  710. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  711. BEGIN
  712. resolved := ResolveType(procedureType.returnType);
  713. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  714. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  715. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  716. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  717. END;
  718. procedureType.SetReturnType(resolved);
  719. IF (resolved # NIL) THEN
  720. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  721. parameter.SetType(procedureType.returnType);
  722. parameter.SetAccess(SyntaxTree.Hidden);
  723. parameter.SetUntraced(procedureType.hasUntracedReturn);
  724. VisitParameter(parameter);
  725. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  726. END;
  727. (* process parameters *)
  728. parameter :=procedureType.firstParameter;
  729. WHILE (parameter # NIL) DO
  730. VisitParameter(parameter);
  731. parameter := parameter.nextParameter;
  732. END;
  733. END FixProcedureType;
  734. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  735. VAR prev,this: SyntaxTree.Modifier;
  736. BEGIN
  737. this := modifiers;prev := NIL;
  738. WHILE (this # NIL) & (this.identifier # name) DO
  739. prev := this; this := this.nextModifier;
  740. END;
  741. IF this # NIL THEN
  742. IF this.expression # NIL THEN
  743. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  744. END;
  745. this.Resolved;
  746. position := this.position;
  747. RETURN TRUE
  748. ELSE
  749. RETURN FALSE
  750. END;
  751. END HasFlag;
  752. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  753. VAR prev,this: SyntaxTree.Modifier;
  754. BEGIN
  755. this := modifiers;prev := NIL;
  756. WHILE (this # NIL) & (this.identifier # name) DO
  757. prev := this; this := this.nextModifier;
  758. END;
  759. IF this # NIL THEN
  760. IF this.expression = NIL THEN
  761. Error(this.position,Diagnostics.Invalid,"expected expression value");
  762. ELSE
  763. this.SetExpression(ConstantExpression(this.expression));
  764. IF CheckIntegerValue(this.expression,value) THEN END;
  765. END;
  766. this.Resolved;
  767. position := this.position;
  768. RETURN TRUE
  769. ELSE RETURN FALSE
  770. END;
  771. END HasValue;
  772. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: ARRAY OF CHAR): BOOLEAN;
  773. VAR prev,this: SyntaxTree.Modifier;
  774. BEGIN
  775. this := modifiers;prev := NIL;
  776. WHILE (this # NIL) & (this.identifier # name) DO
  777. prev := this; this := this.nextModifier;
  778. END;
  779. IF this # NIL THEN
  780. IF this.expression = NIL THEN
  781. Error(this.position,Diagnostics.Invalid,"expected expression value");
  782. ELSE
  783. this.SetExpression(ConstantExpression(this.expression));
  784. IF CheckStringValue(this.expression,value) THEN END;
  785. END;
  786. this.Resolved;
  787. position := this.position;
  788. RETURN TRUE
  789. ELSE RETURN FALSE
  790. END;
  791. END HasStringValue;
  792. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  793. VAR svalue: ARRAY 32 OF CHAR; position: LONGINT;
  794. BEGIN
  795. IF cellsAreObjects THEN RETURN FALSE END;
  796. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  797. IF svalue = "A2" THEN
  798. RETURN TRUE
  799. END;
  800. END;
  801. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  802. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  803. END;
  804. RETURN FALSE;
  805. (*
  806. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  807. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  808. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  809. IF svalue[0] = "~" THEN
  810. Strings.TrimLeft(svalue, "~");
  811. IF svalue = backendName THEN
  812. RETURN TRUE;
  813. END;
  814. ELSIF svalue # backendName THEN
  815. RETURN TRUE;
  816. END;
  817. END;
  818. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  819. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  820. END;
  821. RETURN FALSE;
  822. *)
  823. END SkipImplementation;
  824. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  825. VAR this: SyntaxTree.Modifier;
  826. BEGIN
  827. this := modifiers;
  828. WHILE this # NIL DO
  829. IF ~this.resolved THEN
  830. IF checkUse THEN
  831. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  832. ELSE
  833. this.SetExpression(ResolveExpression(this.expression));
  834. this.Resolved;
  835. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  836. END;
  837. END;
  838. this := this.nextModifier
  839. END;
  840. END CheckModifiers;
  841. (**
  842. resolve procedure type
  843. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  844. **)
  845. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  846. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  847. BEGIN
  848. IF TypeNeedsResolution(procedureType) THEN
  849. modifiers := procedureType.modifiers;
  850. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  851. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  852. procedureType.SetInterrupt(TRUE);
  853. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  854. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  855. IF useDarwinCCalls THEN (*fld*)
  856. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  857. ELSE
  858. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  859. END
  860. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  861. procedureType.SetNoReturn(TRUE);
  862. END;
  863. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  864. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  865. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  866. CheckModifiers(modifiers, TRUE);
  867. modifiers := procedureType.returnTypeModifiers;
  868. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  869. CheckModifiers(modifiers, TRUE);
  870. typeFixes.Add(procedureType,currentScope);
  871. procedureType.SetHasPointers(procedureType.isDelegate);
  872. procedureType.SetState(SyntaxTree.Resolved);
  873. END;
  874. resolvedType := ResolvedType(procedureType)
  875. END VisitProcedureType;
  876. (** check and resolve record type
  877. - check base type: must be record, math array or array-structured object type
  878. - check declarations
  879. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  880. **)
  881. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  882. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  883. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  884. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  885. hasPointers: BOOLEAN;
  886. modifiers: SyntaxTree.Modifier;
  887. value: LONGINT;
  888. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  889. BEGIN
  890. type := type.resolved;
  891. IF (type IS SyntaxTree.PointerType) &
  892. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  893. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  894. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  895. RETURN TRUE
  896. ELSE
  897. RETURN FALSE
  898. END;
  899. END IsPointerToRecord;
  900. BEGIN
  901. IF TypeNeedsResolution(x) THEN
  902. hasPointers := FALSE;
  903. modifiers := x.modifiers;
  904. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  905. CheckModifiers(modifiers, TRUE);
  906. IF x.baseType # NIL THEN
  907. position := x.baseType.position;
  908. baseType := ResolveType(x.baseType);
  909. resolved := baseType.resolved;
  910. hasPointers := hasPointers OR resolved.hasPointers;
  911. IF x.isObject THEN (* object *)
  912. ASSERT(x.pointerType # NIL);
  913. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  914. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  915. baseType := NIL
  916. ELSIF IsPointerToRecord(resolved,recordType) THEN
  917. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  918. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  919. ELSE
  920. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  921. END;
  922. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  923. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  924. ELSIF IsPointerToRecord(resolved,recordType) THEN
  925. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  926. ELSIF resolved IS SyntaxTree.RecordType THEN
  927. ELSE
  928. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  929. END;
  930. ELSE
  931. IF resolved IS SyntaxTree.RecordType THEN
  932. ELSE
  933. Error(position, Diagnostics.Invalid,"record does not extend record")
  934. END;
  935. END;
  936. x.SetBaseType(baseType);
  937. IF x.Level() > 15 THEN
  938. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  939. (* note:
  940. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  941. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  942. inheritance history of a type.
  943. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  944. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  945. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  946. *)
  947. END;
  948. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  949. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  950. END;
  951. END;
  952. Declarations(x.recordScope, FALSE);
  953. ResolveArrayStructure(x);
  954. (* computation of sizes and offsets skipped -> done in backend / system *)
  955. recordBase := x.GetBaseRecord();
  956. IF recordBase = NIL THEN numberMethods := 0
  957. ELSE numberMethods := recordBase.recordScope.numberMethods
  958. END;
  959. isRealtime := TRUE;
  960. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  961. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  962. END;
  963. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  964. WHILE symbol # NIL DO
  965. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  966. IF symbol IS SyntaxTree.Variable THEN
  967. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  968. END;
  969. IF symbol IS SyntaxTree.Procedure THEN
  970. procedure := symbol(SyntaxTree.Procedure);
  971. IF procedure.super # NIL THEN
  972. procedure.SetMethodNumber(procedure.super.methodNumber)
  973. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  974. procedure.SetMethodNumber(numberMethods);
  975. INC(numberMethods);
  976. END;
  977. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  978. Error(procedure.position, Diagnostics.Invalid,"realtime procedure in non-realtime object")
  979. END;
  980. END;
  981. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  982. Error(symbol.position, Diagnostics.Invalid,"non-realtime symbol in realtime object")
  983. END;
  984. symbol := symbol.nextSymbol;
  985. END;
  986. IF isRealtime THEN x.SetRealtime(TRUE) END;
  987. x.recordScope.SetNumberMethods(numberMethods);
  988. (* TODO: is this needed anymore? *)
  989. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  990. Error(x.position,Diagnostics.Invalid,"object extends a record")
  991. END;
  992. IF (x.typeDeclaration = NIL) THEN
  993. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  994. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  995. (*
  996. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  997. AnonymousTypeDeclaration(x,name);
  998. *)
  999. ELSE
  1000. AnonymousTypeDeclaration(x,"Anonymous");
  1001. END;
  1002. END;
  1003. x.SetHasPointers(hasPointers);
  1004. x.SetState(SyntaxTree.Resolved);
  1005. END;
  1006. resolvedType := ResolvedType(x);
  1007. END VisitRecordType;
  1008. (** check and resolve cell type
  1009. - check base type: must be cell
  1010. - check declarations
  1011. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1012. **)
  1013. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1014. VAR
  1015. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1016. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1017. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1018. recordBase: SyntaxTree.RecordType;
  1019. numberMethods, int: LONGINT;
  1020. real: LONGREAL;
  1021. bool: BOOLEAN;
  1022. set: SET;
  1023. v: SyntaxTree.Expression;
  1024. str: Scanner.StringType;
  1025. atype: SyntaxTree.ArrayType;
  1026. prev: SyntaxTree.Scope;
  1027. skip: BOOLEAN;
  1028. svalue: ARRAY 32 OF CHAR;
  1029. BEGIN
  1030. IF TypeNeedsResolution(x) THEN
  1031. recordBase := NIL;
  1032. IF cellsAreObjects THEN
  1033. IF x.baseType = NIL THEN
  1034. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1035. ImportModule(qualifiedIdentifier.prefix, x.position);
  1036. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1037. x.SetBaseType(ResolveType(x.baseType));
  1038. recordBase := x.GetBaseRecord();
  1039. IF recordBase = NIL THEN
  1040. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1041. END;
  1042. ELSE
  1043. x.SetBaseType(ResolveType(x.baseType));
  1044. END;
  1045. ELSE
  1046. x.SetBaseType(ResolveType(x.baseType));
  1047. END;
  1048. IF recordBase = NIL THEN numberMethods := 0
  1049. ELSE numberMethods := recordBase.recordScope.numberMethods
  1050. END;
  1051. modifier := x.modifiers;
  1052. (*IF ~x.isCellNet THEN*)
  1053. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1054. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1055. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1056. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1057. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1058. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1059. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1060. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1061. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1062. symbol := system.activeCellsCapabilities;
  1063. WHILE symbol # NIL DO
  1064. IF HasFlag(modifier, symbol.name, position) THEN END;
  1065. symbol := symbol.nextSymbol;
  1066. END;
  1067. modifier := x.modifiers;
  1068. WHILE (modifier # NIL) DO
  1069. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1070. IF modifier.expression # NIL THEN
  1071. v := ConstantExpression(modifier.expression);
  1072. property.SetValue(v);
  1073. IF IsIntegerValue(modifier.expression, int) THEN
  1074. (*property.SetValue(modifier.expression);*)
  1075. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1076. property.SetType(system.longintType);
  1077. ELSIF IsRealValue(modifier.expression, real) THEN
  1078. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1079. property.SetType(system.longrealType);
  1080. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1081. property.SetType(system.booleanType);
  1082. ELSIF IsSetValue(modifier.expression, set) THEN
  1083. property.SetType(system.setType);
  1084. ELSIF IsStringValue(modifier.expression, str) THEN
  1085. (*property.SetValue(modifier.expression);*)
  1086. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1087. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1088. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1089. property.SetType(atype);
  1090. ELSE
  1091. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1092. END;
  1093. ELSE (* flag property *)
  1094. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1095. property.SetType(system.booleanType);
  1096. END;
  1097. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1098. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1099. x.AddProperty(property);
  1100. modifier := modifier.nextModifier;
  1101. END;
  1102. CheckModifiers(modifier, FALSE);
  1103. Declarations(x.cellScope, SkipImplementation(x));
  1104. (* process parameters *)
  1105. prev := currentScope;
  1106. currentScope := x.cellScope;
  1107. parameter :=x.firstParameter;
  1108. WHILE (parameter # NIL) DO
  1109. VisitParameter(parameter);
  1110. type := parameter.type.resolved;
  1111. IF ~(type IS SyntaxTree.PortType) THEN
  1112. WHILE IsStaticArray(type, type, len) DO
  1113. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1114. END;
  1115. WHILE IsDynamicArray(type, type) DO
  1116. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1117. END;
  1118. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1119. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1120. END;
  1121. END;
  1122. parameter := parameter.nextParameter;
  1123. END;
  1124. currentScope := prev;
  1125. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1126. WHILE symbol # NIL DO
  1127. IF symbol IS SyntaxTree.Variable THEN
  1128. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1129. END;
  1130. symbol := symbol.nextSymbol;
  1131. END;
  1132. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1133. IF (x.typeDeclaration = NIL) THEN
  1134. AnonymousTypeDeclaration(x,"Anonymous");
  1135. END;
  1136. x.SetState(SyntaxTree.Resolved);
  1137. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1138. Warning(x.position, "Forbidden empty Body.");
  1139. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1140. Warning(x.position, "Non-empty body for an engine?");
  1141. END;
  1142. END;
  1143. resolvedType := ResolvedType(x);
  1144. END VisitCellType;
  1145. (* check if an object is an array-structured object type
  1146. - determine the array structure
  1147. - collect operators from top to bottom in the inheritance hierarchy
  1148. - check if LEN operator is declared
  1149. - determine number of possible index operators
  1150. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1151. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1152. *)
  1153. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1154. VAR
  1155. indexOperatorCount, i: LONGINT;
  1156. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1157. isTensor: BOOLEAN;
  1158. BEGIN
  1159. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1160. (* determine array structure *)
  1161. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1162. END;
  1163. IF recordType.HasArrayStructure() THEN
  1164. (* the object is an ASOT *)
  1165. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1166. (* reset array access operators *)
  1167. arrayAccessOperators.len := NIL;
  1168. arrayAccessOperators.generalRead := NIL;
  1169. arrayAccessOperators.generalWrite := NIL;
  1170. IF isTensor THEN
  1171. (* all operators of dimensionalities 1 to max *)
  1172. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1173. ELSE
  1174. (* all operators of certain dimensionality *)
  1175. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1176. END;
  1177. NEW(arrayAccessOperators.read, indexOperatorCount);
  1178. NEW(arrayAccessOperators.write, indexOperatorCount);
  1179. FOR i := 0 TO indexOperatorCount - 1 DO
  1180. arrayAccessOperators.read[i] := NIL;
  1181. arrayAccessOperators.write[i] := NIL
  1182. END;
  1183. (* collect access operators in the record scope *)
  1184. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1185. IF arrayAccessOperators.len = NIL THEN
  1186. (* TODO: think about making this operator optional for static array structures *)
  1187. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1188. END;
  1189. (* show error messages *)
  1190. IF isTensor THEN
  1191. (* require ARRAY [*] OF RANGE *)
  1192. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1193. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1194. ELSE
  1195. (* forbid ARRAY [*] OF RANGE *)
  1196. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1197. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1198. (* require RANGE, RANGE, ... RANGE *)
  1199. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1200. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1201. END;
  1202. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1203. ELSE
  1204. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1205. IF recordType.recordScope.firstOperator # NIL THEN
  1206. RETURN;
  1207. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1208. END
  1209. END
  1210. END ResolveArrayStructure;
  1211. (** collect array access operators in a record scope **)
  1212. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1213. VAR
  1214. baseType: SyntaxTree.Type;
  1215. operator: SyntaxTree.Operator;
  1216. isReadOperator, isGeneralOperator: BOOLEAN;
  1217. indexListSize, indexListKind, hashValue: LONGINT;
  1218. BEGIN
  1219. (* if a parent record scope exists, collect the operators there first *)
  1220. baseType := recordScope.ownerRecord.baseType;
  1221. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1222. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1223. END;
  1224. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1225. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1226. END;
  1227. (* go through all operators in the current record scope *)
  1228. operator := recordScope.firstOperator;
  1229. WHILE operator # NIL DO
  1230. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1231. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1232. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1233. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1234. IF isGeneralOperator THEN
  1235. IF isReadOperator THEN
  1236. arrayAccessOperators.generalRead := operator
  1237. ELSE
  1238. arrayAccessOperators.generalWrite := operator
  1239. END
  1240. ELSE
  1241. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1242. IF isReadOperator THEN
  1243. arrayAccessOperators.read[hashValue] := operator
  1244. ELSE
  1245. arrayAccessOperators.write[hashValue] := operator
  1246. END
  1247. END
  1248. END
  1249. ELSE
  1250. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1251. END;
  1252. operator := operator.nextOperator
  1253. END
  1254. END CollectArrayAccessOperators;
  1255. (** the hash value of an index operator **)
  1256. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1257. VAR result: LONGINT;
  1258. BEGIN
  1259. IF isTensor THEN
  1260. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1261. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1262. ELSE
  1263. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1264. END
  1265. ELSE
  1266. result := indexListKind
  1267. END;
  1268. RETURN result
  1269. END IndexOperatorHash;
  1270. (** 2 to the power of exponent **)
  1271. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1272. VAR result, i: LONGINT;
  1273. BEGIN
  1274. result := 1;
  1275. FOR i := 1 TO exponent DO
  1276. result := result * 2;
  1277. END;
  1278. RETURN result
  1279. END TwoToThePowerOf;
  1280. (** check if a LEN operator has a correct signature. i.e.
  1281. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1282. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1283. **)
  1284. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1285. VAR
  1286. procedureType: SyntaxTree.ProcedureType;
  1287. returnedArrayType: SyntaxTree.MathArrayType;
  1288. result: BOOLEAN;
  1289. BEGIN
  1290. result := FALSE;
  1291. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1292. IF (procedureType.numberParameters = 0) THEN
  1293. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1294. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1295. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1296. IF returnedArrayType.form = SyntaxTree.Open THEN
  1297. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1298. result := TRUE
  1299. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1300. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1301. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1302. result := TRUE
  1303. END
  1304. END
  1305. END
  1306. END
  1307. END;
  1308. IF result THEN
  1309. (* export symbol automatically *)
  1310. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1311. ELSE
  1312. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1313. END;
  1314. RETURN result
  1315. END CheckLenOperator;
  1316. (** check if an index operator has a correct signature. i.e.
  1317. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1318. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1319. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1320. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1321. - determine if it is a read or write operator (existance of return type)
  1322. - check index parameters
  1323. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1324. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1325. [LONGINT] -> binary 0 -> 0
  1326. [RANGE] -> binary 1 -> 1
  1327. [LONGINT, LONGINT] -> binary 00 -> 0
  1328. [LONGINT, RANGE] -> binary 01 -> 1
  1329. [RANGE, LONGINT] -> binary 10 -> 2
  1330. [RANGE, RANGE] -> binary 11 -> 3
  1331. etc.
  1332. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1333. - for read operators, check if return type matches the type of data that is read
  1334. - for write operators, check if last parameter type matches the type of data that is written
  1335. **)
  1336. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1337. VAR
  1338. elementType, otherElementType, dataType: SyntaxTree.Type;
  1339. procedureType: SyntaxTree.ProcedureType;
  1340. mathArrayType: SyntaxTree.MathArrayType;
  1341. parameter: SyntaxTree.Parameter;
  1342. parameterCount, rangeCount, i: LONGINT;
  1343. hasTypeError: BOOLEAN;
  1344. BEGIN
  1345. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1346. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1347. (* determine if it is a read or write operator *)
  1348. isReadOperator := (procedureType.returnType # NIL);
  1349. IF isReadOperator THEN
  1350. indexListSize := parameterCount;
  1351. ELSE
  1352. indexListSize := parameterCount - 1;
  1353. END;
  1354. IF indexListSize < 1 THEN
  1355. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1356. RETURN FALSE
  1357. END;
  1358. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1359. (* general operator *)
  1360. isGeneralOperator := TRUE;
  1361. IF indexListSize > 1 THEN
  1362. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1363. RETURN FALSE
  1364. END;
  1365. (* ARRAY [*] OF RANGE*)
  1366. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1367. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1368. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1369. RETURN FALSE
  1370. END;
  1371. parameter := procedureType.firstParameter.nextParameter
  1372. ELSE
  1373. (* fixed-dim. operator *)
  1374. isGeneralOperator := FALSE;
  1375. (* check number of index parameters *)
  1376. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1377. (* for tensors, limited to a certain size *)
  1378. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1379. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1380. RETURN FALSE
  1381. END
  1382. ELSE
  1383. (* for non-tensors, depends on dimensionality *)
  1384. IF indexListSize # arrayStructure.Dimensionality() THEN
  1385. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1386. RETURN FALSE
  1387. END
  1388. END;
  1389. (* go through all index parameters
  1390. - count the number of ranges
  1391. - determine the index list kind number
  1392. *)
  1393. indexListKind := 0;
  1394. rangeCount := 0;
  1395. parameter := procedureType.firstParameter;
  1396. FOR i := 1 TO indexListSize DO
  1397. indexListKind := indexListKind * 2;
  1398. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1399. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1400. INC(indexListKind);
  1401. INC(rangeCount)
  1402. ELSE
  1403. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1404. RETURN FALSE
  1405. END;
  1406. parameter := parameter.nextParameter
  1407. END;
  1408. END;
  1409. (*
  1410. - for read operators: check type of last parameter
  1411. - for write operators: check return type
  1412. *)
  1413. IF isReadOperator THEN
  1414. dataType := procedureType.returnType (* the return type *)
  1415. ELSE
  1416. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1417. END;
  1418. elementType := arrayStructure.ElementType();
  1419. hasTypeError := FALSE;
  1420. IF isGeneralOperator THEN
  1421. (* ARRAY [?] OF <Element> *)
  1422. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1423. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1424. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1425. hasTypeError := TRUE
  1426. END
  1427. ELSE
  1428. hasTypeError := TRUE
  1429. END
  1430. ELSE
  1431. IF rangeCount = 0 THEN
  1432. (* <Element> *)
  1433. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1434. ELSE
  1435. (* ARRAY [*, *, ..., *] OF <Element> *)
  1436. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1437. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1438. IF mathArrayType.IsFullyDynamic() THEN
  1439. IF mathArrayType.Dimensionality() = rangeCount THEN
  1440. otherElementType := mathArrayType.ElementType();
  1441. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1442. ELSE
  1443. hasTypeError := TRUE
  1444. END
  1445. ELSE
  1446. hasTypeError := TRUE
  1447. END
  1448. ELSE
  1449. hasTypeError := TRUE
  1450. END
  1451. END
  1452. END;
  1453. IF hasTypeError THEN
  1454. IF isReadOperator THEN
  1455. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1456. ELSE
  1457. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1458. END;
  1459. RETURN FALSE
  1460. END;
  1461. (* export symbol automatically *)
  1462. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1463. RETURN TRUE
  1464. END CheckIndexOperator;
  1465. (** resolve all pending types (late resolving).
  1466. - type fixes are resolved at the end of the declaration phase
  1467. - type fixes may imply new type fixes that are also entered at the end of the list
  1468. **)
  1469. PROCEDURE FixTypes;
  1470. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1471. BEGIN
  1472. prevScope := currentScope;
  1473. p := typeFixes.Get(currentScope);
  1474. WHILE p # NIL DO
  1475. ASSERT(currentScope # NIL);
  1476. ASSERT(p IS SyntaxTree.Type);
  1477. IF p IS SyntaxTree.PointerType THEN
  1478. FixPointerType(p(SyntaxTree.PointerType))
  1479. ELSIF p IS SyntaxTree.ProcedureType THEN
  1480. FixProcedureType(p(SyntaxTree.ProcedureType))
  1481. ELSE
  1482. HALT(100);
  1483. END;
  1484. p := typeFixes.Get(currentScope);
  1485. END;
  1486. currentScope :=prevScope;
  1487. END FixTypes;
  1488. (**
  1489. resolve type x
  1490. - if x is nil then return nil
  1491. - if x cannot be resolved then the result is invalidType else the result is x
  1492. - the resolved type is entered into x.resolved
  1493. **)
  1494. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1495. VAR prev,resolved: SyntaxTree.Type;
  1496. BEGIN
  1497. prev := resolvedType;
  1498. resolvedType := SyntaxTree.invalidType;
  1499. IF x = NIL THEN resolvedType := NIL
  1500. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1501. END;
  1502. resolved := resolvedType;
  1503. resolvedType := prev;
  1504. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1505. RETURN resolved
  1506. END ResolveType;
  1507. (*** compatibility rules ***)
  1508. (**
  1509. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1510. **)
  1511. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1512. VAR result: SyntaxTree.Type;
  1513. BEGIN
  1514. result := SyntaxTree.invalidType;
  1515. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1516. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1517. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1518. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1519. ELSE result := type.resolved
  1520. END;
  1521. RETURN result
  1522. END RegularType;
  1523. (** returns signature compatibility of procedure types this and to
  1524. - if not compatible then error is reported
  1525. - compatibility means type equality
  1526. **)
  1527. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1528. VAR result: BOOLEAN;
  1529. BEGIN
  1530. result := SameType(to,this);
  1531. IF ~result THEN
  1532. Error(position,Diagnostics.Invalid,"signature incompatible");
  1533. IF VerboseErrorMessage THEN
  1534. Printout.Info("this",this);
  1535. Printout.Info("to",to);
  1536. END;
  1537. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1538. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1539. END;
  1540. RETURN result
  1541. END SignatureCompatible;
  1542. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1543. - for var parameters compatibility means same type except for
  1544. - formal is of open array of system byte
  1545. - formal is of record type
  1546. - formal is of open array type
  1547. - formal is of open math array type
  1548. - for value parameters compatibllity means assignment compatibility except for
  1549. - formal is of open array type
  1550. if compatible the return true else report error and return false
  1551. **)
  1552. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1553. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1554. BEGIN
  1555. formalType := RegularType(formal.position,formal.type);
  1556. actualType := RegularType(actual.position,actual.type);
  1557. error := FALSE;
  1558. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1559. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1560. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1561. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1562. END;
  1563. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1564. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1565. ELSIF ~IsVariable(actual) THEN
  1566. result := FALSE; error := TRUE;
  1567. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1568. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1569. ELSE
  1570. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1571. END;
  1572. IF VerboseErrorMessage THEN
  1573. Printout.Info("actual",actual);
  1574. Printout.Info("formal",formal);
  1575. END;
  1576. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1577. result := CompatibleTo(system,actualType,formalType);
  1578. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1579. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1580. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1581. IF IsArrayStructuredObjectType(actualType) THEN
  1582. actualType := MathArrayStructureOfType(actualType)
  1583. END;
  1584. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1585. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1586. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1587. END;
  1588. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1589. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1590. END;
  1591. ELSE
  1592. result := SameType(actualType,formalType)
  1593. END
  1594. ELSE
  1595. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1596. actualType := system.characterType;
  1597. END;
  1598. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1599. result := TRUE; (* special rule for WINAPI parameters *)
  1600. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1601. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1602. ELSE
  1603. result := CompatibleTo(system,actualType,formalType);
  1604. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1605. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1606. END;
  1607. END;
  1608. END;
  1609. IF ~result & ~error THEN
  1610. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1611. IF VerboseErrorMessage THEN
  1612. Printout.Info("actual",actual);
  1613. Printout.Info("formal",formal);
  1614. END;
  1615. END;
  1616. RETURN result
  1617. END ParameterCompatible;
  1618. (** check compatibility for expressions of the form left := right
  1619. - if compatible then return true else error report and return false
  1620. - check if left is variable
  1621. - check compatiblity
  1622. **)
  1623. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1624. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1625. BEGIN
  1626. result := FALSE;
  1627. leftType := RegularType(left.position,left.type);
  1628. rightType := RegularType(right.position,right.type);
  1629. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1630. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1631. END;
  1632. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1633. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1634. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1635. (* error already handled *)
  1636. result := TRUE;
  1637. ELSIF ~IsVariable(left) THEN
  1638. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1639. IF VerboseErrorMessage THEN
  1640. Printout.Info("left",left);
  1641. Printout.Info("right",right);
  1642. END;
  1643. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1644. result := TRUE;
  1645. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1646. result := TRUE
  1647. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1648. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1649. IF VerboseErrorMessage THEN
  1650. Printout.Info("left",left);
  1651. Printout.Info("right",right);
  1652. END;
  1653. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1654. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1655. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1656. ELSE
  1657. result := TRUE
  1658. END;
  1659. RETURN result
  1660. END AssignmentCompatible;
  1661. (*** values ***)
  1662. (** check and resolve integer value **)
  1663. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1664. VAR hugeint: HUGEINT;
  1665. BEGIN
  1666. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1667. value.SetType(Global.GetIntegerType(system,hugeint));
  1668. resolvedExpression := value
  1669. END VisitIntegerValue;
  1670. (** check and resolve real value **)
  1671. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1672. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1673. BEGIN
  1674. subtype := value(SyntaxTree.RealValue).subtype;
  1675. IF subtype = Scanner.Real THEN
  1676. type := system.realType
  1677. ELSIF subtype = Scanner.Longreal THEN
  1678. type := system.longrealType
  1679. ELSE
  1680. HALT(100)
  1681. END;
  1682. value.SetType(type);
  1683. resolvedExpression := value
  1684. END VisitRealValue;
  1685. (** check and resolve complex value **)
  1686. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1687. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1688. BEGIN
  1689. subtype := value(SyntaxTree.ComplexValue).subtype;
  1690. IF subtype = Scanner.Real THEN
  1691. type := system.complexType
  1692. ELSIF subtype = Scanner.Longreal THEN
  1693. type := system.longcomplexType
  1694. ELSE
  1695. HALT(100)
  1696. END;
  1697. value.SetType(type);
  1698. resolvedExpression := value
  1699. END VisitComplexValue;
  1700. (** check and resolve set value **)
  1701. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1702. BEGIN
  1703. value.SetType(system.setType);
  1704. resolvedExpression := value
  1705. END VisitSetValue;
  1706. (** check and resolve set value **)
  1707. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1708. BEGIN
  1709. value.SetType(SyntaxTree.invalidType);
  1710. resolvedExpression := value
  1711. END VisitMathArrayValue;
  1712. (** check and resolve boolean value **)
  1713. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1714. BEGIN
  1715. value.SetType(system.booleanType);
  1716. resolvedExpression := value
  1717. END VisitBooleanValue;
  1718. (** check and resolve string value **)
  1719. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1720. BEGIN
  1721. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1722. resolvedExpression := value
  1723. END VisitStringValue;
  1724. (** check and resolve character value **)
  1725. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1726. BEGIN
  1727. value.SetType(system.characterType);
  1728. resolvedExpression := value
  1729. END VisitCharacterValue;
  1730. (** check and resolve nil value **)
  1731. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1732. BEGIN
  1733. value.SetType(system.nilType);
  1734. resolvedExpression := value
  1735. END VisitNilValue;
  1736. (** check and resolve enumerator value **)
  1737. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1738. BEGIN
  1739. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1740. ASSERT(value.type # NIL);
  1741. resolvedExpression := value
  1742. END VisitEnumerationValue;
  1743. (*** expressions ***)
  1744. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1745. - check all elements on integer type
  1746. - if element range is constant, then check lower and upper bound
  1747. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1748. if an error occurs then report error and return invalidExpression
  1749. **)
  1750. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1751. VAR
  1752. i: LONGINT;
  1753. element: SyntaxTree.Expression;
  1754. constant: BOOLEAN;
  1755. elements: SyntaxTree.ExpressionList;
  1756. s: SET;
  1757. result: SyntaxTree.Expression;
  1758. value: SyntaxTree.Value;
  1759. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1760. VAR
  1761. left, right: SyntaxTree.Expression;
  1762. elementResult: SyntaxTree.Expression;
  1763. leftInteger, rightInteger, temp: LONGINT;
  1764. BEGIN
  1765. (* set context of range *)
  1766. IF element IS SyntaxTree.RangeExpression THEN
  1767. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1768. END;
  1769. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1770. IF elementResult = SyntaxTree.invalidExpression THEN
  1771. (* error already reported *)
  1772. constant := FALSE
  1773. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1774. (* the element is a range expression *)
  1775. (* extract left and right hand side of range *)
  1776. left := elementResult(SyntaxTree.RangeExpression).first;
  1777. right := elementResult(SyntaxTree.RangeExpression).last;
  1778. (* guaranteed by VisitRangeExpression: *)
  1779. ASSERT((left # NIL) & (right # NIL));
  1780. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1781. ELSE
  1782. (* the element is not a range expression *)
  1783. (* check type and add conversion if needed *)
  1784. IF IsIntegerType(elementResult.type.resolved) THEN
  1785. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1786. ELSE
  1787. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1788. elementResult := SyntaxTree.invalidExpression;
  1789. constant := FALSE
  1790. END;
  1791. left := elementResult;
  1792. right := elementResult
  1793. END;
  1794. IF elementResult # SyntaxTree.invalidExpression THEN
  1795. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1796. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1797. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1798. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1799. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1800. END
  1801. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1802. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1803. ELSE
  1804. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1805. s := {};
  1806. ELSE
  1807. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1808. IF leftInteger < 0 THEN leftInteger := 0 END;
  1809. (*!!!!!!!!! this is a hack !!!!!!! *)
  1810. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1811. extends the range x..31 to x..63 !!!!!! *)
  1812. s := s + {leftInteger..rightInteger};
  1813. END;
  1814. END;
  1815. ELSE
  1816. constant := FALSE;
  1817. END
  1818. END;
  1819. RETURN elementResult
  1820. END CheckElement;
  1821. BEGIN
  1822. result := set; constant := TRUE; s := {}; elements := set.elements;
  1823. IF elements # NIL THEN
  1824. FOR i := 0 TO elements.Length()-1 DO
  1825. element := elements.GetExpression(i);
  1826. element := CheckElement(element);
  1827. IF element = SyntaxTree.invalidExpression THEN
  1828. result := SyntaxTree.invalidExpression
  1829. END;
  1830. elements.SetExpression(i,element);
  1831. END;
  1832. END;
  1833. IF constant THEN
  1834. value := SyntaxTree.NewSetValue(set.position,s);
  1835. value.SetType(system.setType);
  1836. result.SetResolved(value);
  1837. END;
  1838. (* optimization possible
  1839. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1840. left this to the programmer...
  1841. *)
  1842. result.SetType(system.setType);
  1843. resolvedExpression := result;
  1844. END VisitSet;
  1845. (*
  1846. old variant: quite generic but needs better conversion handling, do this?
  1847. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1848. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1849. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1850. BEGIN
  1851. type := NIL;
  1852. numberElements := x.elements.Length();
  1853. FOR i := 0 TO numberElements-1 DO
  1854. expression := x.elements.GetExpression(i);
  1855. position := expression.position;
  1856. expression := ResolveExpression(x.elements.GetExpression(i));
  1857. x.elements.SetExpression(i,de);
  1858. IF type = NIL THEN
  1859. type := expression.type;
  1860. ELSIF CompatibleTo(system,expression.type,type) THEN
  1861. (* ok *)
  1862. ELSIF CompatibleTo(system,type,expression.type) THEN
  1863. type := expression.type
  1864. ELSE
  1865. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1866. type := SyntaxTree.invalidType;
  1867. END;
  1868. END;
  1869. isValue := TRUE;
  1870. FOR i := 0 TO numberElements-1 DO
  1871. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1872. x.elements.SetExpression(i,expression);
  1873. isValue := isValue & (expression.resolved # NIL);
  1874. END;
  1875. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1876. arrayType.SetArrayBase(type);
  1877. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1878. IF isValue THEN
  1879. value := SyntaxTree.NewMathArrayValue(position);
  1880. value.SetElements(x.elements);
  1881. x.SetResolved(value);
  1882. END;
  1883. x.SetType(arrayType);
  1884. resolvedExpression := x;
  1885. END VisitMathArrayExpression;
  1886. *)
  1887. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1888. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1889. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1890. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1891. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1892. BEGIN
  1893. numberElements := x.elements.Length();
  1894. FOR i := 0 TO numberElements-1 DO
  1895. expression := x.elements.GetExpression(i);
  1896. IF expression IS SyntaxTree.MathArrayExpression THEN
  1897. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1898. ELSE
  1899. position := expression.position;
  1900. expression := ResolveExpression(x.elements.GetExpression(i));
  1901. x.elements.SetExpression(i,expression);
  1902. IF type = NIL THEN
  1903. type := expression.type;
  1904. ELSIF CompatibleTo(system,expression.type,type) THEN
  1905. (* ok *)
  1906. ELSIF CompatibleTo(system,type,expression.type) THEN
  1907. type := expression.type
  1908. ELSE
  1909. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1910. type := SyntaxTree.invalidType;
  1911. END;
  1912. END;
  1913. END;
  1914. END RecursivelyFindType;
  1915. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1916. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1917. BEGIN
  1918. numberElements := x.elements.Length();
  1919. FOR i := 0 TO numberElements-1 DO
  1920. expression := x.elements.GetExpression(i);
  1921. IF expression IS SyntaxTree.MathArrayExpression THEN
  1922. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1923. ELSE
  1924. position := expression.position;
  1925. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1926. x.elements.SetExpression(i,expression);
  1927. isValue := isValue & (expression.resolved # NIL);
  1928. END;
  1929. END;
  1930. END RecursivelySetExpression;
  1931. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1932. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1933. arrayType: SyntaxTree.MathArrayType;
  1934. BEGIN
  1935. numberElements := x.elements.Length();
  1936. baseType := NIL;
  1937. gsize := 0;
  1938. FOR i := 0 TO numberElements-1 DO
  1939. expression := x.elements.GetExpression(i);
  1940. IF expression IS SyntaxTree.MathArrayExpression THEN
  1941. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1942. IF i=0 THEN
  1943. gsize := size;
  1944. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1945. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1946. ELSE expression.SetType(baseType)
  1947. END;
  1948. ELSIF baseType = NIL THEN baseType := type;
  1949. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1950. END;
  1951. END;
  1952. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1953. arrayType.SetArrayBase(baseType);
  1954. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1955. RETURN ResolveType(arrayType);
  1956. END RecursivelySetType;
  1957. BEGIN
  1958. type := NIL;
  1959. RecursivelyFindType(x);
  1960. isValue := TRUE;
  1961. RecursivelySetExpression(x);
  1962. arrayType := RecursivelySetType(x);
  1963. x.SetType(arrayType);
  1964. IF isValue THEN
  1965. value := SyntaxTree.NewMathArrayValue(x.position);
  1966. value.SetArray(x);
  1967. x.SetResolved(value);
  1968. value.SetType(arrayType);
  1969. END;
  1970. x.SetType(arrayType);
  1971. resolvedExpression := x;
  1972. END VisitMathArrayExpression;
  1973. (** check and resolve unary expression **)
  1974. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1975. VAR
  1976. left: SyntaxTree.Expression;
  1977. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1978. bool: BOOLEAN;
  1979. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1980. value: SyntaxTree.Value;
  1981. BEGIN
  1982. type := SyntaxTree.invalidType;
  1983. left := ResolveExpression(unaryExpression.left);
  1984. unaryExpression.SetLeft(left);
  1985. operator := unaryExpression.operator;
  1986. result := unaryExpression;
  1987. IF ~system.operatorDefined[operator] THEN
  1988. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1989. RETURN
  1990. ELSIF left.type = NIL THEN
  1991. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1992. resolvedExpression := SyntaxTree.invalidExpression;
  1993. RETURN
  1994. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1995. RETURN
  1996. END;
  1997. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1998. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1999. END;
  2000. IF operatorCall # NIL THEN
  2001. result := operatorCall;
  2002. type := operatorCall.type;
  2003. (* admissible operators
  2004. Minus number, set
  2005. Not boolean
  2006. *)
  2007. ELSE
  2008. CASE unaryExpression.operator OF
  2009. |Scanner.Minus:
  2010. IF IsIntegerType(left.type.resolved) THEN
  2011. IF left.resolved # NIL THEN
  2012. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2013. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2014. result.SetResolved(value);
  2015. type := Global.GetIntegerType(system,int);
  2016. value.SetType(type);
  2017. ELSE
  2018. type := left.type
  2019. END
  2020. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2021. IF IsRealValue(left,real) THEN
  2022. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2023. result.SetResolved(value);
  2024. type := left.type;
  2025. value.SetType(type);
  2026. ELSE
  2027. type := left.type;
  2028. END;
  2029. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2030. IF IsSetValue(left,set) THEN
  2031. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2032. result.SetResolved(value);
  2033. type := left.type;
  2034. value.SetType(type);
  2035. ELSE
  2036. type := left.type;
  2037. END;
  2038. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2039. IF IsComplexValue(left, real, imaginary) THEN
  2040. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2041. result.SetResolved(value);
  2042. type := left.type;
  2043. value.SetType(type);
  2044. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2045. ELSE
  2046. type := left.type;
  2047. END
  2048. ELSE
  2049. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2050. END;
  2051. |Scanner.Not:
  2052. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2053. IF IsBooleanValue(left,bool) THEN
  2054. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2055. result.SetResolved(value);
  2056. type := system.booleanType;
  2057. value.SetType(type);
  2058. ELSE
  2059. type := system.booleanType;
  2060. END;
  2061. ELSE
  2062. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2063. END;
  2064. |Scanner.Plus:
  2065. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2066. result := left; type := left.type;
  2067. ELSE
  2068. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2069. END;
  2070. (* ADDRESS OF *)
  2071. |Scanner.Address:
  2072. IF HasAddress(left) THEN
  2073. type := system.addressType;
  2074. ELSE
  2075. type := SyntaxTree.invalidType;
  2076. Error(left.position,Diagnostics.Invalid,"has no address");
  2077. Printout.Info("par", left);
  2078. END;
  2079. (* SIZE OF *)
  2080. |Scanner.Size:
  2081. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2082. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2083. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2084. value := SyntaxTree.NewIntegerValue(left.position, int);
  2085. result.SetResolved(value);
  2086. type := Global.GetIntegerType(system,int);
  2087. value.SetType(type)
  2088. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2089. ELSE
  2090. (* for variables, system sizeof could represent the physically occupied size
  2091. determined via the type descriptor, implement that ? *)
  2092. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2093. END
  2094. (* ALIAS OF *)
  2095. |Scanner.Alias:
  2096. type := left.type.resolved;
  2097. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2098. type := SyntaxTree.invalidType;
  2099. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2100. END;
  2101. ELSE
  2102. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2103. END;
  2104. END;
  2105. result.SetType(type);
  2106. resolvedExpression := result
  2107. END VisitUnaryExpression;
  2108. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2109. VAR
  2110. result: SyntaxTree.Expression;
  2111. array: SyntaxTree.MathArrayExpression;
  2112. value: SyntaxTree.MathArrayValue;
  2113. isValue: BOOLEAN;
  2114. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2115. BEGIN
  2116. type := type.resolved;
  2117. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2118. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2119. END;
  2120. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2121. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2122. END;
  2123. RETURN type
  2124. END BaseType;
  2125. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2126. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2127. BEGIN
  2128. numberElements := x.elements.Length();
  2129. FOR i := 0 TO numberElements-1 DO
  2130. expression := x.elements.GetExpression(i);
  2131. IF expression IS SyntaxTree.MathArrayExpression THEN
  2132. array := SyntaxTree.NewMathArrayExpression(position);
  2133. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2134. expression := array;
  2135. ELSE
  2136. position := expression.position;
  2137. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2138. isValue := isValue & (expression.resolved # NIL);
  2139. END;
  2140. to.elements.AddExpression(expression);
  2141. END;
  2142. END RecursivelyConvert;
  2143. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2144. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2145. arrayType: SyntaxTree.MathArrayType;
  2146. BEGIN
  2147. numberElements := x.elements.Length();
  2148. baseType := NIL;
  2149. gsize := 0;
  2150. FOR i := 0 TO numberElements-1 DO
  2151. expression := x.elements.GetExpression(i);
  2152. IF expression IS SyntaxTree.MathArrayExpression THEN
  2153. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2154. IF i=0 THEN
  2155. gsize := size;
  2156. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2157. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2158. ELSE expression.SetType(baseType)
  2159. END;
  2160. ELSIF baseType = NIL THEN baseType := type;
  2161. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2162. END;
  2163. END;
  2164. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2165. arrayType.SetArrayBase(baseType);
  2166. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2167. RETURN ResolveType(arrayType);
  2168. END RecursivelySetType;
  2169. BEGIN
  2170. result := SyntaxTree.invalidExpression;
  2171. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2172. result := expression (* do not convert *)
  2173. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2174. isValue := TRUE;
  2175. type := BaseType(type);
  2176. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2177. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2178. value := SyntaxTree.NewMathArrayValue(array.position);
  2179. value.SetArray(array);
  2180. value.SetType(RecursivelySetType(array));
  2181. result := value;
  2182. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2183. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2184. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2185. IF result = NIL THEN
  2186. result := SyntaxTree.invalidExpression;
  2187. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2188. IF VerboseErrorMessage THEN
  2189. Printout.Info("expression",expression);
  2190. Printout.Info("type",type);
  2191. END;
  2192. END;
  2193. END;
  2194. RETURN result
  2195. END MathArrayConversion;
  2196. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2197. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2198. BEGIN
  2199. result := expression; type := type.resolved;
  2200. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2201. (* skip, no conversion *)
  2202. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2203. int := expression(SyntaxTree.IntegerValue).hvalue;
  2204. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2205. int := Global.ConvertSigned(int,system.SizeOf(type));
  2206. result := SyntaxTree.NewIntegerValue(position,int);
  2207. result.SetType(type);
  2208. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2209. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2210. result := SyntaxTree.NewIntegerValue(position,int);
  2211. result.SetType(type);
  2212. ELSIF (type IS SyntaxTree.FloatType) THEN
  2213. result := SyntaxTree.NewRealValue(expression.position,int);
  2214. result.SetType(type);
  2215. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2216. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2217. result.SetType(type);
  2218. ELSIF (type IS SyntaxTree.SetType) THEN
  2219. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2222. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2223. result.SetType(type);
  2224. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2225. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2226. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2227. END;
  2228. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2229. result.SetType(type);
  2230. ELSIF (type IS SyntaxTree.PortType) THEN
  2231. result := ConvertValue(position, expression, system.integerType);
  2232. ELSE
  2233. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2234. result := SyntaxTree.invalidExpression;
  2235. IF VerboseErrorMessage THEN
  2236. Printout.Info("expression",expression);
  2237. Printout.Info("type",type);
  2238. END;
  2239. END;
  2240. ELSIF IsRealValue(expression,real) THEN
  2241. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2242. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2243. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2244. result.SetType(type);
  2245. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2246. int := ENTIERH(real);
  2247. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2248. result.SetType(type);
  2249. ELSIF (type IS SyntaxTree.FloatType) THEN
  2250. result := SyntaxTree.NewRealValue(position,real);
  2251. result.SetType(type);
  2252. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2253. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2254. result.SetType(type);
  2255. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2256. ELSIF (type IS SyntaxTree.PortType) THEN
  2257. result := ConvertValue(position, expression, system.integerType);
  2258. ELSE
  2259. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2260. result := SyntaxTree.invalidExpression;
  2261. END
  2262. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2263. IF (type IS SyntaxTree.ComplexType) THEN
  2264. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2265. result.SetType(type);
  2266. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2267. ELSE
  2268. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2269. result := SyntaxTree.invalidExpression;
  2270. END
  2271. ELSIF IsSetValue(expression,set) THEN
  2272. IF (type IS SyntaxTree.IntegerType) THEN
  2273. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2274. result.SetType(type);
  2275. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2276. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2277. result.SetType(type);
  2278. ELSIF (type IS SyntaxTree.PortType) THEN
  2279. result := ConvertValue(position, expression, system.integerType);
  2280. ELSE
  2281. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2282. result := SyntaxTree.invalidExpression;
  2283. END;
  2284. ELSIF IsStringValue(expression,string) THEN
  2285. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2286. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2287. result.SetType(type);
  2288. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2289. ELSE
  2290. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2291. result := SyntaxTree.invalidExpression;
  2292. END;
  2293. ELSIF IsCharacterValue(expression,char) THEN
  2294. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2295. string[0] := char; string[1] := 0X;
  2296. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2297. result := SyntaxTree.NewStringValue(expression.position,string);
  2298. result.SetType(type);
  2299. ELSIF (type IS SyntaxTree.ByteType) THEN
  2300. (* do not simply set the new type as this could invalidate types of constants *)
  2301. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2302. result.SetType(type)
  2303. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2304. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2305. result.SetType(type);
  2306. ELSIF (type IS SyntaxTree.SetType) THEN
  2307. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2308. result.SetType(type);
  2309. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2310. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2311. result.SetType(type);
  2312. ELSIF (type IS SyntaxTree.PortType) THEN
  2313. result := ConvertValue(position, expression, system.integerType);
  2314. ELSE
  2315. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2316. result := SyntaxTree.invalidExpression;
  2317. END;
  2318. ELSIF expression IS SyntaxTree.NilValue THEN
  2319. IF type IS SyntaxTree.AddressType THEN
  2320. result := SyntaxTree.NewIntegerValue(position,0);
  2321. result.SetType(type);
  2322. ELSE
  2323. result := expression;
  2324. END;
  2325. (* nothing to be done *)
  2326. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2327. result := MathArrayConversion(position, expression,type);
  2328. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2329. int := expression(SyntaxTree.EnumerationValue).value;
  2330. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2331. int := Global.ConvertSigned(int,system.SizeOf(type));
  2332. result := SyntaxTree.NewIntegerValue(position,int);
  2333. result.SetType(type);
  2334. ELSE
  2335. result := expression;
  2336. END;
  2337. (* nothing to be done *)
  2338. ELSE
  2339. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2340. IF VerboseErrorMessage THEN
  2341. Printout.Info("expression",expression);
  2342. Printout.Info("type",type);
  2343. END;
  2344. result := SyntaxTree.invalidExpression;
  2345. END;
  2346. RETURN result
  2347. END ConvertValue;
  2348. (**
  2349. return a conversion of an expression to a given type
  2350. - if expression is already of same type then return expression
  2351. - if incompatible conversion then report error and return invalidExpression
  2352. **)
  2353. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2354. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2355. BEGIN
  2356. type := type.resolved;
  2357. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2358. result := expression;
  2359. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2360. ELSIF expression = NIL THEN (* NIL expression *)
  2361. ELSIF expression.type = NIL THEN
  2362. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2363. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2364. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2365. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2366. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2367. ELSIF expression.resolved # NIL THEN (* value *)
  2368. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2369. IF value IS SyntaxTree.Value THEN
  2370. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2371. result.SetResolved(value(SyntaxTree.Value));
  2372. result.SetType(value.type);
  2373. ELSE
  2374. result := value
  2375. END;
  2376. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2377. expressionList := SyntaxTree.NewExpressionList();
  2378. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2379. typeDeclaration.SetDeclaredType(type);
  2380. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2381. typeSymbol.SetType(typeDeclaration.type);
  2382. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2383. expressionList.AddExpression(expression);
  2384. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2385. result.SetType(type);
  2386. ELSIF IsArrayStructuredObjectType(type) THEN
  2387. (* no type can be converted to an array-structured object type *)
  2388. HALT(100)
  2389. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2390. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2391. result := MathArrayConversion(position, expression,type);
  2392. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2393. expression := ConvertToMathArray(expression);
  2394. type := MathArrayStructureOfType(type);
  2395. result := MathArrayConversion(position, expression, type)
  2396. ELSE
  2397. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2398. END;
  2399. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2400. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2401. OR ~(type IS SyntaxTree.ArrayType) THEN
  2402. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2403. END;
  2404. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2405. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2406. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2407. (*skip, no conversion*)
  2408. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2409. (* skip, no conversion *)
  2410. ELSE
  2411. ASSERT(~(type IS SyntaxTree.RangeType));
  2412. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2413. ASSERT(type # NIL);
  2414. END;
  2415. RETURN result
  2416. END NewConversion;
  2417. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2418. BEGIN
  2419. IF CompatibleTo(system,expression.type, type) THEN
  2420. RETURN NewConversion(position, expression, type, NIL);
  2421. ELSE
  2422. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2423. RETURN SyntaxTree.invalidExpression
  2424. END;
  2425. END CompatibleConversion;
  2426. (**
  2427. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2428. **)
  2429. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2430. VAR leftType,rightType: SyntaxTree.Type;
  2431. BEGIN
  2432. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2433. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2434. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2435. (* error already handled *)
  2436. ELSE
  2437. leftType := left.type.resolved; rightType := right.type.resolved;
  2438. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2439. right := NewConversion(right.position, right, leftType, NIL);
  2440. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2441. left := NewConversion(left.position,left,rightType,NIL);
  2442. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2443. right := NewConversion(right.position, right, leftType, NIL);
  2444. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2445. left := NewConversion(left.position,left,rightType,NIL);
  2446. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2447. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2448. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2449. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2450. ELSIF
  2451. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2452. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2453. (* must be the case LONGREAL / COMPLEX ) *)
  2454. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2455. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2456. ELSE
  2457. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2458. END;
  2459. END;
  2460. END ConvertOperands;
  2461. (** find and return best operator matching to parameter list (nil, if none)
  2462. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2463. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2464. **)
  2465. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2466. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2467. identifier: SyntaxTree.Identifier;
  2468. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2469. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2470. BEGIN
  2471. operator := scope.firstOperator;
  2472. WHILE(operator # NIL) DO
  2473. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2474. procedureType := operator.type(SyntaxTree.ProcedureType);
  2475. distance := Distance(system, procedureType,actualParameters);
  2476. IF (distance < Infinity) THEN
  2477. IF returnType # NIL THEN
  2478. IF procedureType.returnType = NIL THEN
  2479. distance := Infinity
  2480. ELSE
  2481. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2482. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2483. END;
  2484. END;
  2485. END;
  2486. (*
  2487. IF distance < Infinity THEN
  2488. TRACE(distance, operator);
  2489. Printout.Info("potential operator",operator);
  2490. ELSE
  2491. Printout.Info("operator not possible",operator);
  2492. END;
  2493. *)
  2494. IF distance < bestDistance THEN
  2495. bestDistance := distance;
  2496. bestOperator := operator;
  2497. END;
  2498. END;
  2499. operator := operator.nextOperator;
  2500. END;
  2501. (*
  2502. Printout.Info("taken operator",bestOperator);
  2503. *)
  2504. END FindInScope;
  2505. BEGIN
  2506. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2507. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2508. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2509. import := currentScope.ownerModule.moduleScope.firstImport;
  2510. WHILE (bestDistance > 0) & (import # NIL) DO
  2511. IF import.module # NIL THEN
  2512. identifier := Global.GetIdentifier(operator,import.module.case);
  2513. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2514. END;
  2515. import := import.nextImport;
  2516. END;
  2517. RETURN bestOperator
  2518. END FindOperator;
  2519. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2520. BEGIN
  2521. currentScope := scope;
  2522. END SetCurrentScope;
  2523. (**
  2524. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2525. - handle LEN and DIM operator for array-structured object types
  2526. - find operator, if found then
  2527. - if in other module then add import designator
  2528. - create symbol designator for operator
  2529. - if error then return invalidExpression, if no operator then return NIL
  2530. **)
  2531. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2532. VAR
  2533. operator: SyntaxTree.Operator;
  2534. import: SyntaxTree.Import;
  2535. expression, result: SyntaxTree.Expression;
  2536. designator: SyntaxTree.Designator;
  2537. actualParameters, tempList: SyntaxTree.ExpressionList;
  2538. recordType: SyntaxTree.RecordType;
  2539. castReturnType : SyntaxTree.MathArrayType;
  2540. BEGIN
  2541. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2542. result := SyntaxTree.invalidExpression
  2543. ELSIF leftExpression = NIL THEN
  2544. result := NIL
  2545. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2546. (* LEN or DIM operator on array-structured object type *)
  2547. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2548. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2549. IF recordType.arrayAccessOperators.len = NIL THEN
  2550. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2551. result := SyntaxTree.invalidExpression
  2552. ELSE
  2553. ASSERT(leftExpression IS SyntaxTree.Designator);
  2554. designator := leftExpression(SyntaxTree.Designator);
  2555. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2556. ASSERT(expression IS SyntaxTree.Designator);
  2557. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2558. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2559. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2560. result := designator
  2561. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2562. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2563. tempList := SyntaxTree.NewExpressionList();
  2564. tempList.AddExpression(rightExpression);
  2565. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2566. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2567. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2568. tempList := SyntaxTree.NewExpressionList();
  2569. tempList.AddExpression(designator);
  2570. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2571. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2572. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2573. END
  2574. END;
  2575. ELSE
  2576. (* import OCArrayBase if needed *)
  2577. IF ~arrayBaseImported THEN
  2578. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2579. (* operators on complex numbers *)
  2580. ImportModule(Global.ArrayBaseName,position);
  2581. arrayBaseImported := TRUE;
  2582. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2583. IF op = Global.Dim THEN
  2584. (* not existing in OCArrayBase *)
  2585. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2586. (* not existing in OCArrayBase *)
  2587. ELSE
  2588. ImportModule(Global.ArrayBaseName,position);
  2589. arrayBaseImported := TRUE;
  2590. END
  2591. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2592. ImportModule(Global.ArrayBaseName,position);
  2593. arrayBaseImported := TRUE
  2594. END;
  2595. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2596. (* LEN(RANGE) *)
  2597. ImportModule(Global.ArrayBaseName,position);
  2598. arrayBaseImported := TRUE;
  2599. END;
  2600. END;
  2601. actualParameters := SyntaxTree.NewExpressionList();
  2602. actualParameters.AddExpression(leftExpression);
  2603. IF rightExpression # NIL THEN
  2604. actualParameters.AddExpression(rightExpression)
  2605. END;
  2606. operator := FindOperator(system,op,actualParameters,resultType);
  2607. IF operator # NIL THEN
  2608. designator := NIL;
  2609. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2610. import := currentScope.ownerModule.moduleScope.firstImport;
  2611. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2612. import := import.nextImport;
  2613. END;
  2614. expression := NewSymbolDesignator(position,NIL,import);
  2615. designator := expression(SyntaxTree.Designator);
  2616. END;
  2617. expression := NewSymbolDesignator(position,designator,operator);
  2618. designator := expression(SyntaxTree.Designator);
  2619. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2620. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2621. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2622. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2623. result.SetType(castReturnType);
  2624. END;
  2625. ELSE
  2626. result := NIL;
  2627. END;
  2628. END;
  2629. RETURN result
  2630. END NewOperatorCall;
  2631. (** check and resolve binary expression **)
  2632. (*! clean up *)
  2633. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2634. VAR left,right,result: SyntaxTree.Expression;
  2635. leftType, rightType: SyntaxTree.Type;
  2636. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2637. cl,cr: CHAR;
  2638. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2639. type: SyntaxTree.Type;
  2640. value: SyntaxTree.Value;
  2641. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2642. integerConstantFolding: BOOLEAN;
  2643. list: SyntaxTree.ExpressionList;
  2644. PROCEDURE NewBool(v: BOOLEAN);
  2645. BEGIN
  2646. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2647. value.SetType(system.booleanType);
  2648. result.SetResolved(value);
  2649. type := system.booleanType
  2650. END NewBool;
  2651. PROCEDURE NewSet(v: SET);
  2652. BEGIN
  2653. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2654. value.SetType(system.setType);
  2655. result.SetResolved(value);
  2656. type := system.setType;
  2657. END NewSet;
  2658. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2659. BEGIN
  2660. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2661. (* type cast to "larger" type only if the value is still in the range *)
  2662. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2663. value.SetType(t);
  2664. END;
  2665. result.SetResolved(value);
  2666. type := value.type;
  2667. END NewInteger;
  2668. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2669. BEGIN
  2670. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2671. value.SetType(t);
  2672. result.SetResolved(value);
  2673. type := t;
  2674. END NewReal;
  2675. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2676. BEGIN
  2677. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2678. value.SetType(t);
  2679. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2680. result.SetResolved(value);
  2681. type := t;
  2682. END NewComplex;
  2683. BEGIN
  2684. type := SyntaxTree.invalidType;
  2685. left := ResolveExpression(binaryExpression.left);
  2686. right := ResolveExpression(binaryExpression.right);
  2687. binaryExpression.SetLeft(left);
  2688. binaryExpression.SetRight(right);
  2689. result := binaryExpression;
  2690. operator := binaryExpression.operator;
  2691. IF ~system.operatorDefined[operator] THEN
  2692. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2693. result := SyntaxTree.invalidExpression;
  2694. RETURN
  2695. END;
  2696. IF left.type = NIL THEN
  2697. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2698. result := SyntaxTree.invalidExpression;
  2699. RETURN;
  2700. END;
  2701. IF right.type = NIL THEN
  2702. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2703. result := SyntaxTree.invalidExpression;
  2704. RETURN;
  2705. END;
  2706. leftType := left.type.resolved; rightType := right.type.resolved;
  2707. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2708. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2709. END;
  2710. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2711. list := SyntaxTree.NewExpressionList();
  2712. list.AddExpression(right);
  2713. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2714. END;
  2715. IF operatorCall # NIL THEN
  2716. result := operatorCall;
  2717. type := operatorCall.type;
  2718. (* admissible operators:
  2719. Times, Plus, Minus numeric numeric numeric
  2720. set set set
  2721. Slash numeric numeric real /complex
  2722. set set set
  2723. Div , Mod integer integer integer
  2724. And, Or bool bool bool
  2725. Equal, Unequal basic basic bool
  2726. pointer pointer bool
  2727. object object bool
  2728. record record bool
  2729. string string bool
  2730. enumerator enumerator bool
  2731. Less, LessEqual,
  2732. Greater, GreaterEqual integer/real integer/real bool
  2733. enumerator enumerator bool
  2734. In integer set bool
  2735. Is pointer type bool
  2736. object type bool
  2737. record type bool
  2738. Upto: special abbreviation for a..b
  2739. *)
  2740. ELSIF (left.type = NIL) THEN
  2741. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2742. D.Str("nil type in "); D.Type(left); D.Ln;
  2743. result := SyntaxTree.invalidExpression;
  2744. ELSIF (right.type = NIL) THEN
  2745. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2746. result := SyntaxTree.invalidExpression;
  2747. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2748. result := SyntaxTree.invalidExpression;
  2749. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2750. HALT(100);
  2751. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2752. type := system.booleanType;
  2753. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2754. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2755. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2756. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2757. IF VerboseErrorMessage THEN
  2758. Printout.Info("left",left);
  2759. Printout.Info("right",right);
  2760. END;
  2761. ELSIF IsUnsafePointer(left.type) THEN
  2762. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2763. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2764. NewBool(TRUE)
  2765. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2766. NewBool(TRUE);
  2767. ELSIF IsUnextensibleRecord(left) THEN
  2768. NewBool(FALSE)
  2769. END
  2770. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2771. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2772. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2773. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2774. ELSIF operator = Scanner.In THEN (* left IN right *)
  2775. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2776. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2777. NewBool(il IN sr);
  2778. ELSE
  2779. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2780. left := NewConversion(left.position, left, system.longintType,NIL);
  2781. binaryExpression.SetLeft(left)
  2782. END;
  2783. type := system.booleanType;
  2784. END
  2785. ELSE
  2786. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2787. END
  2788. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2789. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2790. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2791. END;
  2792. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2793. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2794. END
  2795. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2796. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2797. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2798. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2799. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2800. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2801. ConvertOperands(left, right);
  2802. binaryExpression.SetLeft(left);
  2803. binaryExpression.SetRight(right);
  2804. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2805. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2806. END;
  2807. type := system.booleanType;
  2808. ELSE
  2809. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2810. END
  2811. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2812. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2813. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2814. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2815. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2816. CASE operator OF
  2817. |Scanner.Equal: NewBool(strl^=strr^);
  2818. |Scanner.Unequal:NewBool(strl^#strr^);
  2819. |Scanner.Less: NewBool(strl^<strr^);
  2820. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2821. |Scanner.Greater: NewBool(strl^>strr^);
  2822. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2823. ELSE
  2824. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2825. END;
  2826. END;
  2827. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2828. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2829. type := system.booleanType
  2830. ELSE
  2831. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2832. END;
  2833. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2834. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2835. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2836. type := system.booleanType;
  2837. ELSE
  2838. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2839. END
  2840. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2841. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2842. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2843. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2844. type := system.booleanType
  2845. ELSE
  2846. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2847. END;
  2848. ELSE
  2849. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2850. END;
  2851. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2852. type := system.booleanType;
  2853. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2854. type := system.booleanType;
  2855. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2856. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2857. THEN
  2858. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2859. IF (leftType # rightType) THEN
  2860. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2861. ConvertOperands(left,right); (* operands must be of the same type here *)
  2862. END;
  2863. binaryExpression.SetLeft(left);
  2864. binaryExpression.SetRight(right);
  2865. leftType := left.type.resolved;
  2866. rightType := right.type.resolved;
  2867. END;
  2868. type := leftType;
  2869. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2870. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2871. IF VerboseErrorMessage THEN
  2872. Printout.Info("left",left);
  2873. Printout.Info("right",right);
  2874. END;
  2875. ELSIF IsIntegerType(leftType) THEN
  2876. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2877. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2878. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2879. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2880. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2881. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2882. END;
  2883. END;
  2884. (* constant folding *)
  2885. (* bootstrap64
  2886. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2887. CASE operator OF
  2888. |Scanner.Plus: NewInteger(il+ir,left.type);
  2889. |Scanner.Minus: NewInteger(il-ir,left.type);
  2890. |Scanner.Times: NewInteger(il*ir,left.type);
  2891. |Scanner.Slash:
  2892. IF ir # 0 THEN
  2893. NewReal(il/ir, system.realType);
  2894. END;
  2895. |Scanner.Mod:
  2896. IF ir > 0 THEN
  2897. NewInteger(il MOD ir,left.type);
  2898. END;
  2899. |Scanner.Div:
  2900. IF ir > 0 THEN
  2901. NewInteger(il DIV ir,left.type);
  2902. END;
  2903. |Scanner.Equal: NewBool(il=ir);
  2904. |Scanner.Unequal:NewBool(il#ir);
  2905. |Scanner.Less: NewBool(il<ir);
  2906. |Scanner.LessEqual: NewBool(il<=ir);
  2907. |Scanner.Greater: NewBool(il>ir);
  2908. |Scanner.GreaterEqual: NewBool(il>=ir);
  2909. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2910. END;
  2911. ELS*)
  2912. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2913. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2914. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2915. CASE operator OF
  2916. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2917. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2918. |Scanner.Times: NewInteger(hl*hr,left.type);
  2919. |Scanner.Slash:
  2920. IF hr = 0 THEN
  2921. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2922. ELSE
  2923. IF type.sizeInBits = 64 THEN
  2924. NewReal(hl/hr,system.longrealType);
  2925. ELSE
  2926. NewReal(hl/hr,system.realType)
  2927. END
  2928. END;
  2929. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2930. |Scanner.Mod:
  2931. IF hr = 0 THEN
  2932. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2933. ELSE
  2934. NewInteger(hl MOD hr, left.type);
  2935. (* bootstrap64
  2936. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2937. *)
  2938. END;
  2939. |Scanner.Div:
  2940. IF hr = 0 THEN
  2941. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2942. ELSE
  2943. NewInteger(hl DIV hr, left.type);
  2944. (* bootstrap64
  2945. NewInteger(Machine.DivH(hl,hr),left.type);
  2946. *)
  2947. END;
  2948. (* *)
  2949. |Scanner.Equal: NewBool(hl=hr);
  2950. |Scanner.Unequal: NewBool(hl#hr);
  2951. |Scanner.Less: NewBool(hl<hr);
  2952. |Scanner.LessEqual: NewBool(hl<=hr);
  2953. |Scanner.Greater: NewBool(hl>hr);
  2954. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2955. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2956. END;
  2957. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2958. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2959. type := left.type
  2960. ELSIF (operator = Scanner.Slash) THEN
  2961. left := NewConversion(left.position,left,system.realType,NIL);
  2962. right := NewConversion(right.position,right,system.realType,NIL);
  2963. binaryExpression.SetLeft(left);
  2964. binaryExpression.SetRight(right);
  2965. type := system.realType
  2966. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2967. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2968. type := system.booleanType
  2969. ELSE
  2970. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2971. END;
  2972. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2973. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2974. CASE operator OF
  2975. |Scanner.Plus: NewReal(rl+rr,leftType);
  2976. |Scanner.Minus: NewReal(rl-rr,leftType);
  2977. |Scanner.Times:NewReal(rl*rr,leftType);
  2978. |Scanner.Slash:
  2979. IF rr = 0 THEN
  2980. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2981. ELSE
  2982. NewReal(rl/rr,leftType);
  2983. END
  2984. |Scanner.Equal: NewBool(rl=rr);
  2985. |Scanner.Unequal: NewBool(rl#rr);
  2986. |Scanner.Less: NewBool(rl<rr);
  2987. |Scanner.LessEqual: NewBool(rl<=rr);
  2988. |Scanner.Greater: NewBool(rl>rr);
  2989. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2990. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2991. END;
  2992. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2993. type := left.type
  2994. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2995. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2996. type := system.booleanType
  2997. ELSE
  2998. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2999. IF VerboseErrorMessage THEN
  3000. Printout.Info("left",left);
  3001. Printout.Info("right",right);
  3002. END;
  3003. END;
  3004. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3005. CASE operator OF
  3006. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3007. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3008. ELSE
  3009. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  3010. IF VerboseErrorMessage THEN
  3011. Printout.Info("left", left);
  3012. Printout.Info("right", right)
  3013. END;
  3014. END;
  3015. IF ~error THEN
  3016. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3017. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  3018. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3019. (* do constant folding *)
  3020. CASE operator OF
  3021. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3022. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3023. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3024. |Scanner.Slash:
  3025. divisor := c * c + d * d;
  3026. ASSERT(divisor # 0);
  3027. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3028. |Scanner.Equal: NewBool((a = c) & (b = d))
  3029. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3030. END
  3031. END
  3032. END
  3033. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3034. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3035. CASE operator OF
  3036. |Scanner.And: NewBool(bl & br);
  3037. |Scanner.Or: NewBool(bl OR br);
  3038. |Scanner.Equal: NewBool(bl = br);
  3039. |Scanner.Unequal: NewBool(bl # br);
  3040. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3041. END;
  3042. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3043. type := system.booleanType
  3044. ELSE
  3045. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3046. END;
  3047. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3048. (* constant folding *)
  3049. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3050. IF operator = Scanner.Equal THEN
  3051. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3052. ELSIF operator = Scanner.Unequal THEN
  3053. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3054. END;
  3055. END;
  3056. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3057. type := system.booleanType;
  3058. ELSE
  3059. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3060. END;
  3061. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3062. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3063. CASE operator OF
  3064. |Scanner.Plus: NewSet(sl + sr);
  3065. |Scanner.Minus: NewSet(sl - sr);
  3066. |Scanner.Times: NewSet(sl * sr);
  3067. |Scanner.Slash: NewSet(sl / sr);
  3068. |Scanner.Equal: NewBool(sl=sr);
  3069. |Scanner.Unequal: NewBool(sl#sr);
  3070. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3071. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3072. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3073. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3074. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3075. END;
  3076. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3077. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3078. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3079. THEN
  3080. type := system.booleanType
  3081. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3082. type := left.type
  3083. ELSE
  3084. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3085. END;
  3086. ELSIF IsCharacterType(left.type) THEN
  3087. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3088. CASE operator OF
  3089. |Scanner.Equal: NewBool(cl=cr);
  3090. |Scanner.Unequal: NewBool(cl#cr);
  3091. |Scanner.Less: NewBool(cl<cr);
  3092. |Scanner.LessEqual: NewBool(cl<=cr);
  3093. |Scanner.Greater: NewBool(cl>cr);
  3094. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3095. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3096. END;
  3097. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3098. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3099. type := system.booleanType
  3100. ELSE
  3101. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3102. END;
  3103. ELSE
  3104. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3105. END;
  3106. ELSE
  3107. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3108. END;
  3109. IF type = SyntaxTree.invalidType THEN
  3110. result := SyntaxTree.invalidExpression
  3111. ELSE
  3112. result.SetType(type)
  3113. END;
  3114. resolvedExpression := result
  3115. END VisitBinaryExpression;
  3116. (** resolve a range expression of the from <<first .. last BY step>>
  3117. - depending on the context different things are checked:
  3118. ArrayIndex:
  3119. - components must be integers
  3120. - replace missing lower bound with 0
  3121. - replace missing upper bound with MAX(LONGINT)
  3122. - replace missing step size with 1
  3123. SetElement:
  3124. - components must be integers
  3125. - replace missing lower bound with 0
  3126. - replace missing upper bound with MAX(SET)
  3127. - must not have step size
  3128. CaseGuard:
  3129. - components must be constant
  3130. - components must be integers or characters
  3131. - must have lower and upper bound present
  3132. - components are made compatible
  3133. - must not have step size
  3134. - if error: return invalidExpression
  3135. **)
  3136. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3137. VAR
  3138. hasError: BOOLEAN;
  3139. first, last, step: SyntaxTree.Expression;
  3140. BEGIN
  3141. hasError := FALSE;
  3142. first := x.first;
  3143. last := x.last;
  3144. step := x.step;
  3145. (* check lower bound *)
  3146. IF x.context = SyntaxTree.CaseGuard THEN
  3147. IF first = NIL THEN
  3148. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3149. hasError := TRUE
  3150. ELSE
  3151. first := ResolveExpression(first);
  3152. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3153. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3154. hasError := TRUE
  3155. ELSE
  3156. IF first IS SyntaxTree.StringValue THEN
  3157. (* add conversion from string to character *)
  3158. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3159. END
  3160. END;
  3161. (* check if expression is constant *)
  3162. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3163. (* error already reported *)
  3164. hasError := TRUE
  3165. END
  3166. END
  3167. ELSE (* ArrayIndex, SetElement *)
  3168. IF first = NIL THEN
  3169. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3170. END;
  3171. first := ResolveExpression(first);
  3172. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3173. first := NewConversion(first.position, first, system.longintType, NIL)
  3174. ELSE
  3175. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3176. hasError := TRUE
  3177. END
  3178. END;
  3179. (* check upper bound *)
  3180. IF x.context = SyntaxTree.CaseGuard THEN
  3181. IF last = NIL THEN
  3182. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3183. hasError := TRUE
  3184. ELSE
  3185. last := ResolveExpression(last);
  3186. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3187. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3188. hasError := TRUE
  3189. ELSE
  3190. IF last IS SyntaxTree.StringValue THEN
  3191. (* add conversion from string to character *)
  3192. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3193. END
  3194. END;
  3195. (* check if expression is constant *)
  3196. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3197. (* error already reported *)
  3198. hasError := TRUE
  3199. ELSE
  3200. (* try to make lower and upper bound compatible *)
  3201. ConvertOperands(first, last);
  3202. IF first.type.resolved # last.type.resolved THEN
  3203. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3204. hasError := TRUE
  3205. END
  3206. END
  3207. END
  3208. ELSE (* ArrayIndex, SetElement *)
  3209. IF last = NIL THEN
  3210. IF x.context = SyntaxTree.ArrayIndex THEN
  3211. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3212. ELSE
  3213. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3214. END
  3215. END;
  3216. last := ResolveExpression(last);
  3217. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3218. last := NewConversion(last.position, last, system.longintType, NIL)
  3219. ELSE
  3220. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3221. hasError := TRUE
  3222. END
  3223. END;
  3224. (* check step size *)
  3225. IF x.context = SyntaxTree.ArrayIndex THEN
  3226. IF step = NIL THEN
  3227. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3228. END;
  3229. step := ResolveExpression(step);
  3230. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3231. step := NewConversion(step.position, step, system.longintType, NIL)
  3232. ELSE
  3233. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3234. hasError := TRUE
  3235. END
  3236. ELSE (* SetElement, CaseGuard *)
  3237. IF step # NIL THEN
  3238. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3239. hasError := TRUE
  3240. END
  3241. END;
  3242. IF hasError THEN
  3243. resolvedExpression := SyntaxTree.invalidExpression
  3244. ELSE
  3245. x.SetFirst(first);
  3246. x.SetLast(last);
  3247. x.SetStep(step);
  3248. x.SetType(system.rangeType);
  3249. resolvedExpression := x;
  3250. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3251. END
  3252. END VisitRangeExpression;
  3253. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3254. BEGIN
  3255. x.SetType(NIL);
  3256. resolvedExpression := x;
  3257. END VisitTensorRangeExpression;
  3258. (** resolve the expression d and return result as designator
  3259. - resolve expression
  3260. - if expression is a designator then return designator else error message and return invalidDesignator
  3261. **)
  3262. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3263. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3264. BEGIN
  3265. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3266. resolved := ResolveExpression(d);
  3267. IF resolved = SyntaxTree.invalidExpression THEN
  3268. (* error should already have been reported *)
  3269. result := SyntaxTree.invalidDesignator;
  3270. ELSIF resolved IS SyntaxTree.Designator THEN
  3271. result := resolved(SyntaxTree.Designator);
  3272. ELSE
  3273. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3274. result := SyntaxTree.invalidDesignator;
  3275. END;
  3276. (* result.type might be nil. *)
  3277. RETURN result
  3278. END ResolveDesignator;
  3279. (**
  3280. symbol designator generated in this module
  3281. nothing to be resolved
  3282. **)
  3283. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3284. BEGIN
  3285. resolvedExpression := x;
  3286. END VisitSymbolDesignator;
  3287. (**
  3288. self designator generated in this module
  3289. nothing to be resolved
  3290. **)
  3291. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3292. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3293. BEGIN
  3294. (* check if in record scope *)
  3295. scope := currentScope;
  3296. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3297. scope := scope.outerScope;
  3298. END;
  3299. IF scope = NIL THEN (* in module scope *)
  3300. x.SetType(system.anyType);
  3301. ELSIF scope IS SyntaxTree.CellScope THEN
  3302. cell := scope(SyntaxTree.CellScope).ownerCell;
  3303. x.SetType(cell);
  3304. ELSE (* in record scope *)
  3305. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3306. IF (record # NIL) & (record.pointerType # NIL) THEN
  3307. type := ResolveType(record.pointerType);
  3308. x.SetType(type);
  3309. ELSE
  3310. x.SetType(record);
  3311. END;
  3312. END;
  3313. resolvedExpression := x;
  3314. END VisitSelfDesignator;
  3315. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3316. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3317. BEGIN
  3318. scope := currentScope;
  3319. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3320. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3321. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3322. returnType := procedureType.returnType;
  3323. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3324. THEN
  3325. x.SetType(returnType);
  3326. ELSE
  3327. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3328. x.SetType(SyntaxTree.invalidType);
  3329. END;
  3330. ELSE
  3331. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3332. x.SetType(SyntaxTree.invalidType);
  3333. END;
  3334. x.SetAssignable(TRUE);
  3335. resolvedExpression := x;
  3336. END VisitResultDesignator;
  3337. (**
  3338. return symbol designator as an expression
  3339. - if symbol is a constant then return the constant value expression
  3340. - else
  3341. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3342. - if symbol is a guarded variable then return a TypeGuardDesignator
  3343. - else return a symbol designator
  3344. **)
  3345. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3346. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3347. guardType: SyntaxTree.Type;
  3348. BEGIN
  3349. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3350. result := SyntaxTree.invalidExpression;
  3351. ASSERT(symbol # NIL);
  3352. (*
  3353. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3354. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3355. Error(position,Diagnostics.Invalid,"type not allowed here");
  3356. ELS *)
  3357. (* not needed any more as values are stored in the expression
  3358. IF symbol IS SyntaxTree.Constant THEN
  3359. result := symbol(SyntaxTree.Constant).value
  3360. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3361. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3362. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3363. ELSE
  3364. result := symbol(SyntaxTree.Constant).value
  3365. END;
  3366. ELSE
  3367. *)
  3368. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3369. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3370. THEN
  3371. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3372. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3373. left := NewDereferenceDesignator(position,left);
  3374. left.SetHidden(TRUE);
  3375. END;
  3376. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3377. scope := currentScope;
  3378. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3379. scope := scope.outerScope;
  3380. END;
  3381. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3382. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3383. END;
  3384. END;
  3385. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3386. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3387. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3388. ELSE
  3389. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3390. END;
  3391. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3392. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3393. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3394. result.SetType(symbol.type);
  3395. result.SetAssignable(assignable);
  3396. symbol.MarkUsed;
  3397. IF symbol IS SyntaxTree.Constant THEN
  3398. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3399. END;
  3400. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3401. variableAccessed := TRUE
  3402. END;
  3403. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3404. IF GetGuard(symbol,guardType) THEN
  3405. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3406. END;
  3407. END;
  3408. ASSERT(result.type # NIL);
  3409. RETURN result
  3410. END NewSymbolDesignator;
  3411. (** check and resolve an identifier designator "identifier"
  3412. - if identifier = self then return SelfDesignator
  3413. - else find symbol in current scope
  3414. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3415. **)
  3416. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3417. VAR symbol: SyntaxTree.Symbol;
  3418. BEGIN
  3419. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3420. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3421. IF symbol # NIL THEN
  3422. ResolveSymbol(symbol);
  3423. ASSERT(symbol.type # NIL);
  3424. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3425. ELSE
  3426. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3427. IF VerboseErrorMessage THEN
  3428. Printout.Info("undeclared identifier designator",identifierDesignator);
  3429. END;
  3430. resolvedExpression := SyntaxTree.invalidDesignator;
  3431. END;
  3432. END VisitIdentifierDesignator;
  3433. (** check and resolve a selector designator of the form left.designator
  3434. - if left is a pointer type then do auto dereferenciation
  3435. - left denotes a search scope:
  3436. - if left type is import type then set search scope to respective module
  3437. - if left type is enumeration type then set search scope to respective enumeration scope
  3438. - elsif left type is record type then set search scope to record scope
  3439. - search symbol in computed scope
  3440. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3441. **)
  3442. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3443. VAR
  3444. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3445. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3446. BEGIN
  3447. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3448. left := ResolveDesignator(selectorDesignator.left);
  3449. result := SyntaxTree.invalidDesignator;
  3450. IF left # NIL THEN
  3451. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3452. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3453. END;
  3454. scope := NIL;
  3455. IF left.type = NIL THEN
  3456. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3457. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3458. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3459. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3460. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3461. module := symbol(SyntaxTree.Import).module;
  3462. IF module # NIL THEN
  3463. scope := module.moduleScope
  3464. ELSE
  3465. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3466. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3467. END;
  3468. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3469. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3470. ASSERT(scope # NIL)
  3471. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3472. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3473. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3474. IF type IS SyntaxTree.EnumerationType THEN
  3475. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3476. ELSE
  3477. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3478. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3479. END;
  3480. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3481. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3482. ELSE
  3483. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3484. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3485. END;
  3486. symbol := NIL;
  3487. IF scope # NIL THEN
  3488. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3489. IF symbol # NIL THEN
  3490. ResolveSymbol(symbol);
  3491. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3492. symbol.MarkUsed
  3493. ELSE
  3494. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3495. IF VerboseErrorMessage THEN
  3496. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3497. Printout.Info("scope", scope);
  3498. Printout.Info("left", left);
  3499. Printout.Info("undeclared identifier",selectorDesignator);
  3500. Printout.Info("left resolved designator",left);
  3501. END
  3502. END;
  3503. END;
  3504. END;
  3505. resolvedExpression := result;
  3506. END VisitSelectorDesignator;
  3507. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3508. VAR len,idx: LONGINT;
  3509. BEGIN
  3510. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3511. IF idx < 0 THEN
  3512. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3513. ELSE
  3514. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3515. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3516. END;
  3517. END;
  3518. END;
  3519. END IndexCheck;
  3520. (*
  3521. - if index designator has not type, use newBaseType as its type
  3522. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3523. - special rule: if static array of dynamic array occurs, make it all dynamic
  3524. index designator type: new base type: new index designator type:
  3525. NIL z z
  3526. ARRAY [x, y] z ARRAY [x, y] OF z
  3527. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3528. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3529. *)
  3530. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3531. VAR
  3532. mathArrayType: SyntaxTree.MathArrayType;
  3533. makeDynamic: BOOLEAN;
  3534. BEGIN
  3535. IF indexDesignator.type = NIL THEN
  3536. indexDesignator.SetType(newBaseType)
  3537. ELSE
  3538. (* index designator must be a of math array type *)
  3539. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3540. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3541. (* determine if all arrays have to be made dynamic *)
  3542. makeDynamic :=
  3543. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3544. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3545. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3546. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3547. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3548. END;
  3549. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3550. mathArrayType.SetArrayBase(newBaseType)
  3551. END
  3552. END SetIndexBaseType;
  3553. (** check and append index list element to index designator of math array
  3554. - check validity of single index or array range
  3555. - compute new type
  3556. - if range then create new array type (calculate length of resulting array)
  3557. - otherwise take sourceArray.arrayBase as new type
  3558. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3559. **)
  3560. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3561. VAR
  3562. targetArray: SyntaxTree.MathArrayType;
  3563. first, last, step: SyntaxTree.Expression;
  3564. firstValue, lastValue, stepValue, length: LONGINT;
  3565. rangeExpression: SyntaxTree.RangeExpression;
  3566. BEGIN
  3567. IF indexListItem.type = SyntaxTree.invalidType THEN
  3568. (* error already handled *)
  3569. indexDesignator.parameters.AddExpression(indexListItem)
  3570. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3571. indexDesignator.HasRange;
  3572. indexDesignator.HasTensorRange;
  3573. indexDesignator.parameters.AddExpression(indexListItem);
  3574. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3575. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3576. IndexCheck(indexListItem, sourceArray.length);
  3577. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3578. indexDesignator.parameters.AddExpression(indexListItem)
  3579. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3580. indexDesignator.HasRange;
  3581. (* if the range is given as an array range expression, check the validity of its components *)
  3582. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3583. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3584. first := rangeExpression.first;
  3585. last := rangeExpression.last;
  3586. step := rangeExpression.step;
  3587. (* perform static checks on range components *)
  3588. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3589. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3590. END;
  3591. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3592. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3593. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3594. END
  3595. END;
  3596. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3597. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3598. END;
  3599. (* add conversions to size type *)
  3600. (* TODO: needed? *)
  3601. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3602. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3603. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3604. END;
  3605. IF indexDesignator.hasTensorRange THEN
  3606. (* the index designator's base type is a tensor: leave it as is *)
  3607. ELSE
  3608. (* append a new math array to the index designator's base type *)
  3609. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3610. IF ~error THEN
  3611. (*
  3612. (* optimization: calculate length of target array for static ranges *)
  3613. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3614. IF IsStaticallyOpenRange(rangeExpression) THEN
  3615. (* range is open ('*'): reuse source array length as target array length *)
  3616. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3617. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3618. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3619. IF lastValue = MAX(LONGINT) THEN
  3620. IF IsIntegerValue(sourceArray.length, length) THEN
  3621. lastValue := length - 1;
  3622. isStaticTargetArrayLength := TRUE
  3623. ELSE
  3624. isStaticTargetArrayLength := FALSE
  3625. END
  3626. ELSE
  3627. isStaticTargetArrayLength := TRUE
  3628. END;
  3629. IF isStaticTargetArrayLength THEN
  3630. (* calculate static target array length *)
  3631. IF firstValue > lastValue THEN
  3632. length := 0
  3633. ELSE
  3634. length := 1 + lastValue - firstValue;
  3635. IF length MOD stepValue = 0 THEN
  3636. length := length DIV stepValue
  3637. ELSE
  3638. length := length DIV stepValue + 1
  3639. END
  3640. END;
  3641. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3642. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3643. ASSERT(targetArray.form = SyntaxTree.Static)
  3644. END
  3645. END
  3646. END
  3647. *)
  3648. END;
  3649. SetIndexBaseType(indexDesignator, targetArray)
  3650. END;
  3651. indexDesignator.parameters.AddExpression(indexListItem)
  3652. ELSE
  3653. Error(position, Diagnostics.Invalid,"invalid index list item");
  3654. END;
  3655. END AppendMathIndex;
  3656. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3657. VAR parameters: SyntaxTree.ExpressionList;
  3658. BEGIN
  3659. parameters := index.parameters;
  3660. IF (expression.type = NIL) THEN
  3661. Error(position,Diagnostics.Invalid,"invalid index");
  3662. ELSIF IsIntegerType(expression.type.resolved) THEN
  3663. IF over IS SyntaxTree.ArrayType THEN
  3664. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3665. ELSIF over IS SyntaxTree.StringType THEN
  3666. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3667. END;
  3668. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3669. parameters.AddExpression(expression);
  3670. ELSE
  3671. Error(position,Diagnostics.Invalid,"invalid index");
  3672. END;
  3673. END AppendIndex;
  3674. (** convert an expression to math array type
  3675. if expression is of math array type: return expression itself
  3676. if expression is of array-structured object type: return an index operator call on it
  3677. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3678. otherwise: return invalid expression
  3679. **)
  3680. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3681. VAR
  3682. result: SyntaxTree.Expression;
  3683. mathArrayType: SyntaxTree.MathArrayType;
  3684. BEGIN
  3685. IF expression.type = NIL THEN
  3686. result := SyntaxTree.invalidExpression
  3687. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3688. (* expression of math array type *)
  3689. result := expression
  3690. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3691. (* expression of array-structured object type *)
  3692. mathArrayType := MathArrayStructureOfType(expression.type);
  3693. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3694. ELSE
  3695. result := SyntaxTree.invalidExpression
  3696. END;
  3697. RETURN result
  3698. END ConvertToMathArray;
  3699. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3700. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3701. VAR
  3702. result: SyntaxTree.ExpressionList;
  3703. i: LONGINT;
  3704. BEGIN
  3705. result := SyntaxTree.NewExpressionList();
  3706. FOR i := 1 TO itemCount DO
  3707. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3708. END;
  3709. RETURN result
  3710. END ListOfOpenRanges;
  3711. (** create a procedure call designator for an index operator call on an array-structured object type
  3712. - use given index list as actual parameters
  3713. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3714. **)
  3715. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3716. VAR
  3717. operator: SyntaxTree.Operator;
  3718. expression: SyntaxTree.Expression;
  3719. actualParameters, tempList: SyntaxTree.ExpressionList;
  3720. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3721. result, tempDesignator: SyntaxTree.Designator;
  3722. recordType: SyntaxTree.RecordType;
  3723. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3724. i, hashValue, indexListSize, indexListKind: LONGINT;
  3725. castReturnType: SyntaxTree.MathArrayType;
  3726. BEGIN
  3727. ASSERT(IsArrayStructuredObjectType(left.type));
  3728. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3729. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3730. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3731. indexListSize := indexList.Length();
  3732. indexListKind := 0;
  3733. containsNonRange := FALSE;
  3734. FOR i := 0 TO indexList.Length() - 1 DO
  3735. indexListKind := indexListKind * 2;
  3736. expression := indexList.GetExpression(i);
  3737. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3738. INC(indexListKind)
  3739. ELSE
  3740. containsNonRange := TRUE
  3741. END
  3742. END;
  3743. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3744. (* select applicable index operator
  3745. - try to look up optimal index operator
  3746. - if not present, use operator on ranges
  3747. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3748. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3749. *)
  3750. usesGeneralOperator := FALSE;
  3751. IF rhs # NIL THEN
  3752. (* write operator *)
  3753. IF hashValue = -1 THEN
  3754. operator := NIL
  3755. ELSE
  3756. operator := recordType.arrayAccessOperators.write[hashValue];
  3757. END;
  3758. IF operator = NIL THEN
  3759. usesPureRangeOperator := TRUE;
  3760. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3761. operator := recordType.arrayAccessOperators.generalWrite;
  3762. usesGeneralOperator := TRUE
  3763. ELSE
  3764. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3765. operator := recordType.arrayAccessOperators.write[hashValue];
  3766. END
  3767. END
  3768. ELSE
  3769. (* read operator *)
  3770. IF hashValue = -1 THEN
  3771. operator := NIL
  3772. ELSE
  3773. operator := recordType.arrayAccessOperators.read[hashValue];
  3774. END;
  3775. IF operator = NIL THEN
  3776. usesPureRangeOperator := TRUE;
  3777. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3778. operator := recordType.arrayAccessOperators.generalRead;
  3779. usesGeneralOperator := TRUE
  3780. ELSE
  3781. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3782. operator := recordType.arrayAccessOperators.read[hashValue];
  3783. END
  3784. END
  3785. END;
  3786. IF operator = NIL THEN
  3787. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3788. result := SyntaxTree.invalidDesignator;
  3789. ELSE
  3790. (* determine if reshaping is needed *)
  3791. needsReshaping := containsNonRange & usesPureRangeOperator;
  3792. (* import OCArrayBase if reshaping is needed *)
  3793. IF needsReshaping & ~arrayBaseImported THEN
  3794. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3795. arrayBaseImported := TRUE
  3796. END;
  3797. (* add the index list item to the list of actual parameters
  3798. - for general operators: add a single inline array containing the index list items as parameter
  3799. - otherwise: add all index list items as individual parameters
  3800. *)
  3801. actualParameters := SyntaxTree.NewExpressionList();
  3802. IF usesGeneralOperator THEN
  3803. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3804. END;
  3805. FOR i := 0 TO indexListSize - 1 DO
  3806. expression := indexList.GetExpression(i);
  3807. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3808. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3809. tempList := SyntaxTree.NewExpressionList();
  3810. tempList.AddExpression(expression);
  3811. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3812. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3813. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3814. END;
  3815. IF usesGeneralOperator THEN
  3816. tempMathArrayExpression.elements.AddExpression(expression);
  3817. ELSE
  3818. actualParameters.AddExpression(expression)
  3819. END
  3820. END;
  3821. IF usesGeneralOperator THEN
  3822. actualParameters.AddExpression(tempMathArrayExpression)
  3823. END;
  3824. IF rhs # NIL THEN
  3825. (* add actual parameter for RHS *)
  3826. IF needsReshaping THEN
  3827. (* reshape using OCArrayBase.ExpandDimensions *)
  3828. tempList := SyntaxTree.NewExpressionList();
  3829. (* source array *)
  3830. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3831. tempList.AddExpression(rhs);
  3832. ELSE
  3833. (* convert scalar to one-dimensional array *)
  3834. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3835. tempMathArrayExpression.elements.AddExpression(rhs);
  3836. tempList.AddExpression(tempMathArrayExpression)
  3837. END;
  3838. (* list of kept dimensions *)
  3839. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3840. FOR i := 0 TO indexListSize - 1 DO
  3841. expression := indexList.GetExpression(i);
  3842. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3843. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3844. ELSE
  3845. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3846. END
  3847. END;
  3848. tempList.AddExpression(tempMathArrayExpression);
  3849. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3850. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3851. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3852. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3853. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3854. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3855. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3856. expression.SetType(castReturnType);
  3857. ELSE
  3858. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3859. END;
  3860. actualParameters.AddExpression(expression)
  3861. ELSE
  3862. actualParameters.AddExpression(rhs)
  3863. END
  3864. END;
  3865. (* add dereference operator and create procedure call designator *)
  3866. ASSERT(left IS SyntaxTree.Designator);
  3867. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3868. ASSERT(expression IS SyntaxTree.Designator);
  3869. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3870. IF (rhs = NIL) & needsReshaping THEN
  3871. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3872. tempList := SyntaxTree.NewExpressionList();
  3873. FOR i := 0 TO indexList.Length() - 1 DO
  3874. expression := indexList.GetExpression(i);
  3875. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3876. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3877. ELSE
  3878. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3879. END
  3880. END;
  3881. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3882. END;
  3883. IF rhs = NIL THEN
  3884. (* special rule: index read operator calls are considered to be assignable *)
  3885. result.SetAssignable(TRUE)
  3886. END;
  3887. (* put information about this index operator call into the resulting designator *)
  3888. result.SetRelatedAsot(left);
  3889. result.SetRelatedIndexList(indexList)
  3890. END;
  3891. RETURN result
  3892. END NewIndexOperatorCall;
  3893. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3894. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3895. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3896. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3897. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3898. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3899. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3900. CONST trace = FALSE;
  3901. BEGIN
  3902. IF trace THEN
  3903. FOR i := 0 TO actualParameters.Length()-1 DO
  3904. Printout.Info("par", actualParameters.GetExpression(i));
  3905. END;
  3906. END;
  3907. operator := scope.firstOperator;
  3908. WHILE(operator # NIL) DO
  3909. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3910. procedureType := operator.type(SyntaxTree.ProcedureType);
  3911. distance := Distance(system, procedureType,actualParameters);
  3912. IF trace THEN Printout.Info("check op ",operator) END;
  3913. IF distance < bestDistance THEN
  3914. IF trace THEN Printout.Info("taken op",operator) END;
  3915. bestDistance := distance;
  3916. bestOperator := operator;
  3917. END;
  3918. END;
  3919. operator := operator.nextOperator;
  3920. END;
  3921. END FindInScope;
  3922. BEGIN
  3923. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3924. IF oper = 0 THEN (* index *)
  3925. identifier := SyntaxTree.NewIdentifier("[]");
  3926. ELSE
  3927. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3928. END;
  3929. WHILE (recordType # NIL) DO
  3930. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3931. recordType := recordType.GetBaseRecord();
  3932. END;
  3933. RETURN bestOperator
  3934. END FindOperator;
  3935. BEGIN
  3936. type := left.type.resolved;
  3937. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3938. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3939. actualParameters := SyntaxTree.NewExpressionList();
  3940. IF parameters # NIL THEN
  3941. FOR i := 0 TO parameters.Length()-1 DO
  3942. expression := ResolveExpression(parameters.GetExpression(i));
  3943. actualParameters.AddExpression(expression);
  3944. END;
  3945. END;
  3946. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3947. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3948. IF op # NIL THEN
  3949. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3950. ASSERT(expression IS SyntaxTree.Designator);
  3951. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3952. result.SetRelatedAsot(left);
  3953. result.SetRelatedIndexList(parameters);
  3954. (* check if write operator exists, for var parameters *)
  3955. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3956. actualParameters := SyntaxTree.NewExpressionList();
  3957. FOR i := 0 TO parameters.Length()-1 DO
  3958. expression := ResolveExpression(parameters.GetExpression(i));
  3959. actualParameters.AddExpression(expression);
  3960. END;
  3961. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3962. actualParameters.AddExpression(rhs);
  3963. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3964. IF op = NIL THEN rhs := NIL END;
  3965. END;
  3966. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3967. ELSE
  3968. result := NIL;
  3969. END;
  3970. RETURN result;
  3971. END NewObjectOperatorCall;
  3972. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3973. 1. convert bracket designator chains into a single one that contains separators
  3974. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3975. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3976. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3977. - if an array or math array is indexed over, create index designator
  3978. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3979. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3980. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3981. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3982. - if an array-structured object type is indexed over, create procedure call designator
  3983. e.g.: a[x, y] -> a^."[]"(x, y)
  3984. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3985. - a[i, *] = a[i][*]
  3986. - a[*, i] # a[*][i]
  3987. Because:
  3988. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3989. - 'i-th column' = a[*, i]
  3990. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3991. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3992. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3993. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3994. **)
  3995. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3996. VAR
  3997. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3998. indexDesignator: SyntaxTree.IndexDesignator;
  3999. designator: SyntaxTree.Designator;
  4000. type: SyntaxTree.Type;
  4001. recordType: SyntaxTree.RecordType;
  4002. expression, rhs: SyntaxTree.Expression;
  4003. indexList: SyntaxTree.ExpressionList;
  4004. i: LONGINT;
  4005. hasError, done: BOOLEAN;
  4006. PROCEDURE FinalizeIndexDesignator;
  4007. BEGIN
  4008. IF indexDesignator # NIL THEN
  4009. (* the end of a tensor has been reached: *)
  4010. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4011. SetIndexBaseType(indexDesignator, type);
  4012. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4013. designator := indexDesignator;
  4014. type := designator.type.resolved;
  4015. indexDesignator := NIL;
  4016. ASSERT(SyntaxTree.Resolved IN type.state)
  4017. END
  4018. END FinalizeIndexDesignator;
  4019. BEGIN
  4020. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4021. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4022. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4023. (* copy all index list entries including a separator to the left bracket designator *)
  4024. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4025. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4026. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4027. END;
  4028. (* propagate the related RHS *)
  4029. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4030. (* only resolve left bracket designator and use as final result *)
  4031. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4032. ELSE
  4033. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4034. designator := ResolveDesignator(bracketDesignator.left);
  4035. type := designator.type.resolved;
  4036. indexDesignator := NIL;
  4037. (*!!! clean up *)
  4038. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4039. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4040. IF resolvedExpression = NIL THEN
  4041. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4042. resolvedExpression := SyntaxTree.invalidDesignator
  4043. END;
  4044. RETURN;
  4045. END;
  4046. i := 0;
  4047. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4048. expression := bracketDesignator.parameters.GetExpression(i);
  4049. expression := ResolveExpression(expression);
  4050. bracketDesignator.parameters.SetExpression(i, expression);
  4051. IF expression = SyntaxTree.indexListSeparator THEN
  4052. (* finalize an existing index designator if needed *)
  4053. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4054. INC(i)
  4055. ELSE
  4056. (* do auto-dereferencing if needed *)
  4057. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4058. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4059. & (i=0)*)
  4060. THEN
  4061. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4062. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4063. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4064. designator := SyntaxTree.invalidDesignator;
  4065. type := SyntaxTree.invalidType
  4066. ELSE
  4067. FinalizeIndexDesignator;
  4068. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4069. type := designator.type.resolved
  4070. END
  4071. END;
  4072. (* create a new index designator, if needed *)
  4073. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4074. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4075. indexDesignator.SetAssignable(designator.assignable);
  4076. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4077. (* designator := indexDesignator *)
  4078. END;
  4079. IF type = SyntaxTree.invalidType THEN
  4080. (* error already handled *)
  4081. INC(i)
  4082. ELSIF type IS SyntaxTree.ArrayType THEN
  4083. (* indexing over an array *)
  4084. ASSERT(indexDesignator # NIL);
  4085. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4086. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4087. INC(i)
  4088. ELSIF type IS SyntaxTree.StringType THEN
  4089. (* indexing over an array *)
  4090. ASSERT(indexDesignator # NIL);
  4091. AppendIndex(expression.position, indexDesignator, expression, type);
  4092. type := type(SyntaxTree.StringType).baseType.resolved;
  4093. INC(i)
  4094. ELSIF type IS SyntaxTree.MathArrayType THEN
  4095. (* indexing over a math array *)
  4096. ASSERT(indexDesignator # NIL);
  4097. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4098. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4099. INC(i)
  4100. ELSIF IsArrayStructuredObjectType(type) THEN
  4101. (* indexing over ASOTs *)
  4102. FinalizeIndexDesignator;
  4103. ASSERT(type IS SyntaxTree.PointerType);
  4104. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4105. (*
  4106. - collect index list items from bracket designator that belong to ASOT
  4107. - check for errors
  4108. *)
  4109. indexList := SyntaxTree.NewExpressionList();
  4110. hasError := FALSE;
  4111. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4112. (* indexing over tensor ASOT:
  4113. - stop at index list end or separator
  4114. - dimensionality is given by number of index list items
  4115. *)
  4116. done := FALSE;
  4117. WHILE ~done DO
  4118. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4119. done := TRUE;
  4120. ELSE
  4121. expression := bracketDesignator.parameters.GetExpression(i);
  4122. IF expression = SyntaxTree.indexListSeparator THEN
  4123. done := TRUE;
  4124. ELSE
  4125. expression := ResolveExpression(expression);
  4126. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4127. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4128. hasError := TRUE
  4129. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4130. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4131. expression := SyntaxTree.invalidExpression;
  4132. hasError := TRUE
  4133. END;
  4134. indexList.AddExpression(expression)
  4135. END;
  4136. INC(i)
  4137. END
  4138. END
  4139. ELSE
  4140. (* indexing over non-tensor ASOT:
  4141. - ignore separators
  4142. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4143. *)
  4144. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4145. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4146. expression := bracketDesignator.parameters.GetExpression(i);
  4147. ELSE
  4148. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4149. END;
  4150. IF expression # SyntaxTree.indexListSeparator THEN
  4151. expression := ResolveExpression(expression);
  4152. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4153. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4154. expression := SyntaxTree.invalidExpression;
  4155. hasError := TRUE
  4156. END;
  4157. indexList.AddExpression(expression)
  4158. END;
  4159. INC(i)
  4160. END;
  4161. END;
  4162. IF hasError THEN
  4163. designator := SyntaxTree.invalidDesignator;
  4164. type := SyntaxTree.invalidType;
  4165. ELSE
  4166. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4167. and the last entry in the index list belongs to the array-structured object type in question.
  4168. E.g.: for a 2-dimensional array-structured object type:
  4169. - 'lhs := asot[1, 2]' -> read mode
  4170. - 'asot[1, 2] := rhs' -> write mode
  4171. - 'asot[1, 2, 3] := rhs' -> read mode
  4172. *)
  4173. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4174. rhs := bracketDesignator.relatedRhs
  4175. ELSE
  4176. rhs := NIL
  4177. END;
  4178. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4179. type := designator.type
  4180. END
  4181. ELSE
  4182. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4183. designator := SyntaxTree.invalidDesignator;
  4184. type := SyntaxTree.invalidType;
  4185. INC(i)
  4186. END
  4187. END
  4188. END;
  4189. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4190. resolvedExpression := designator
  4191. END
  4192. END VisitBracketDesignator;
  4193. (** check and resolve expression list
  4194. - resolve each expression in an expression list
  4195. - returns true if and only if all statements could have successfully been resolved
  4196. **)
  4197. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4198. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4199. BEGIN
  4200. result := TRUE;
  4201. FOR i := 0 TO expressionList.Length()-1 DO
  4202. expression := ResolveExpression(expressionList.GetExpression(i));
  4203. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4204. expressionList.SetExpression(i,expression);
  4205. END;
  4206. RETURN result
  4207. END ExpressionList;
  4208. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4209. BEGIN
  4210. type := type.resolved;
  4211. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4212. RETURN TRUE
  4213. ELSIF system.CanPassInRegister # NIL THEN
  4214. RETURN system.CanPassInRegister(type);
  4215. ELSE
  4216. RETURN FALSE
  4217. END;
  4218. END CanPassInRegister;
  4219. (** return procedure call designator left(actualParameters)
  4220. - check realtime procedure call in realtime procedure
  4221. - check number of parameters
  4222. - check parameter compatibility
  4223. return invalidDesignator if error
  4224. **)
  4225. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4226. VAR result: SyntaxTree.Designator;
  4227. numberFormalParameters, numberActualParameters: LONGINT;
  4228. formalType: SyntaxTree.ProcedureType;
  4229. formalParameter: SyntaxTree.Parameter;
  4230. actualParameter: SyntaxTree.Expression;
  4231. i: LONGINT;
  4232. BEGIN
  4233. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4234. result := SyntaxTree.invalidDesignator;
  4235. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4236. numberFormalParameters := formalType.numberParameters;
  4237. numberActualParameters := actualParameters.Length();
  4238. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4239. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4240. END;
  4241. IF ~ExpressionList(actualParameters) THEN
  4242. result := SyntaxTree.invalidDesignator
  4243. ELSE
  4244. IF numberActualParameters <= numberFormalParameters THEN
  4245. formalParameter := formalType.firstParameter;
  4246. FOR i := 0 TO numberActualParameters-1 DO
  4247. actualParameter := actualParameters.GetExpression(i);
  4248. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4249. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4250. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4251. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4252. ELSE
  4253. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4254. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4255. END;
  4256. actualParameters.SetExpression(i,actualParameter);
  4257. END;
  4258. formalParameter := formalParameter.nextParameter;
  4259. END;
  4260. WHILE (formalParameter # NIL) DO
  4261. IF formalParameter.defaultValue # NIL THEN
  4262. actualParameters.AddExpression(formalParameter.defaultValue);
  4263. formalParameter := formalParameter.nextParameter
  4264. ELSE
  4265. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4266. formalParameter := NIL;
  4267. END;
  4268. END;
  4269. ELSE
  4270. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4271. END;
  4272. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4273. result.SetAssignable(FALSE);
  4274. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4275. END;
  4276. RETURN result
  4277. END NewProcedureCallDesignator;
  4278. (**
  4279. builtin call designator generated in VisitParameterDesignator
  4280. -> nothing to be resolved
  4281. **)
  4282. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4283. BEGIN
  4284. resolvedExpression := x;
  4285. END VisitTypeGuardDesignator;
  4286. (**
  4287. builtin call designator generated in VisitParameterDesignator
  4288. -> nothing to be resolved
  4289. **)
  4290. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4291. BEGIN
  4292. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4293. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4294. ASSERT(resolvedExpression.type # NIL);
  4295. ELSIF ExpressionList(x.parameters) THEN
  4296. resolvedExpression := x;
  4297. END;
  4298. END VisitBuiltinCallDesignator;
  4299. (**
  4300. procedure call designator generated in VisitParameterDesignator
  4301. -> nothing to be resolved
  4302. **)
  4303. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4304. BEGIN
  4305. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4306. resolvedExpression := x;
  4307. END VisitProcedureCallDesignator;
  4308. (** return true if x is a variable else return false and report error **)
  4309. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4310. VAR result: BOOLEAN;
  4311. BEGIN
  4312. result := TRUE;
  4313. IF x = SyntaxTree.invalidExpression THEN
  4314. result := FALSE;
  4315. ELSIF ~IsVariable(x) THEN
  4316. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4317. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4318. result := FALSE;
  4319. END;
  4320. RETURN result
  4321. END CheckVariable;
  4322. (**
  4323. if expression x is of basic type then return true else report error and return false
  4324. **)
  4325. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4326. VAR result: BOOLEAN;
  4327. BEGIN
  4328. result := FALSE;
  4329. IF x = SyntaxTree.invalidExpression THEN
  4330. ELSIF ~IsBasicType(x.type) THEN
  4331. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4332. result := FALSE
  4333. ELSE result := TRUE
  4334. END;
  4335. RETURN result
  4336. END CheckBasicType;
  4337. (**
  4338. if expression x is of number type then return true else report error and return false
  4339. **)
  4340. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4341. VAR result: BOOLEAN;
  4342. BEGIN
  4343. result := FALSE;
  4344. IF x = SyntaxTree.invalidExpression THEN
  4345. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4346. Error(x.position,Diagnostics.Invalid,"is non number type");
  4347. ELSE result := TRUE
  4348. END;
  4349. RETURN result
  4350. END CheckNumberType;
  4351. (**
  4352. if expression x is of number or size type but not complex then return true else report error and return false
  4353. **)
  4354. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4355. VAR result: BOOLEAN;
  4356. BEGIN
  4357. result := FALSE;
  4358. IF x = SyntaxTree.invalidExpression THEN
  4359. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4360. Error(x.position,Diagnostics.Invalid,"is complex type");
  4361. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4362. Error(x.position,Diagnostics.Invalid,"is non number type");
  4363. ELSE result := TRUE
  4364. END;
  4365. RETURN result
  4366. END CheckNonComplexNumberSizeType;
  4367. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4368. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4369. BEGIN
  4370. result := FALSE; type := x.type.resolved;
  4371. IF x = SyntaxTree.invalidExpression THEN
  4372. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4373. TRACE(type.sizeInBits);
  4374. TRACE(system.addressType.sizeInBits);
  4375. Error(x.position,Diagnostics.Invalid,"is no address type");
  4376. ELSE result := TRUE
  4377. END;
  4378. RETURN result
  4379. END CheckAddressType;
  4380. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4381. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4382. BEGIN
  4383. result := FALSE; type := x.type.resolved;
  4384. IF x = SyntaxTree.invalidExpression THEN
  4385. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4386. Error(x.position,Diagnostics.Invalid,"is no size type");
  4387. ELSE result := TRUE
  4388. END;
  4389. RETURN result
  4390. END CheckSizeType;
  4391. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4392. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4393. BEGIN
  4394. result := FALSE; type := x.type.resolved;
  4395. IF x = SyntaxTree.invalidExpression THEN
  4396. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4397. Error(x.position,Diagnostics.Invalid,"is no object type");
  4398. ELSE result := TRUE
  4399. END;
  4400. RETURN result
  4401. END CheckObjectType;
  4402. (**
  4403. if expression x is of integer type then return true else report error and return false
  4404. **)
  4405. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4406. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4407. BEGIN
  4408. result := FALSE; type := x.type.resolved;
  4409. IF x = SyntaxTree.invalidExpression THEN
  4410. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4411. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4412. ELSE result := TRUE
  4413. END;
  4414. RETURN result
  4415. END CheckIntegerType;
  4416. (**
  4417. if expression x is of character type then return true else report error and return false
  4418. **)
  4419. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4420. VAR result: BOOLEAN;
  4421. BEGIN
  4422. result := FALSE;
  4423. IF x = SyntaxTree.invalidExpression THEN
  4424. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4425. Error(x.position,Diagnostics.Invalid,"is no character type");
  4426. ELSE result := TRUE
  4427. END;
  4428. RETURN result
  4429. END CheckCharacterType;
  4430. (**
  4431. if expression x is of real type then return true else report error and return false
  4432. **)
  4433. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4434. VAR result: BOOLEAN;
  4435. BEGIN
  4436. result := FALSE;
  4437. IF x = SyntaxTree.invalidExpression THEN
  4438. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4439. Error(x.position,Diagnostics.Invalid,"is no float type");
  4440. ELSE result := TRUE
  4441. END;
  4442. RETURN result
  4443. END CheckRealType;
  4444. (**
  4445. if expression x is of range type then return true else report error and return false
  4446. **)
  4447. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4448. VAR result: BOOLEAN;
  4449. BEGIN
  4450. result := FALSE;
  4451. IF x = SyntaxTree.invalidExpression THEN
  4452. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4453. Error(x.position,Diagnostics.Invalid,"is no range type");
  4454. ELSE result := TRUE
  4455. END;
  4456. RETURN result
  4457. END CheckRangeType;
  4458. (**
  4459. if expression x is of boolean type then return true else report error and return false
  4460. **)
  4461. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4462. VAR result: BOOLEAN;
  4463. BEGIN
  4464. result := FALSE;
  4465. IF x = SyntaxTree.invalidExpression THEN
  4466. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4467. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4468. ELSE result := TRUE
  4469. END;
  4470. RETURN result
  4471. END CheckBooleanType;
  4472. (**
  4473. if expression x is of set type then return true else report error and return false
  4474. **)
  4475. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4476. VAR result: BOOLEAN;
  4477. BEGIN
  4478. result := FALSE;
  4479. IF x = SyntaxTree.invalidExpression THEN
  4480. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4481. Error(x.position,Diagnostics.Invalid,"is no set type");
  4482. ELSE result := TRUE
  4483. END;
  4484. RETURN result
  4485. END CheckSetType;
  4486. (**
  4487. if expression x is of string or array of character type then return true else report error and return false
  4488. **)
  4489. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4490. VAR result: BOOLEAN;
  4491. BEGIN
  4492. result := FALSE;
  4493. IF x = SyntaxTree.invalidExpression THEN
  4494. ELSIF ~IsStringType(x.type.resolved) THEN
  4495. Error(x.position,Diagnostics.Invalid,"is no string type");
  4496. ELSE result := TRUE
  4497. END;
  4498. RETURN result
  4499. END CheckStringType;
  4500. (**
  4501. if expression x is a type declaration type return true else report error and return false
  4502. **)
  4503. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4504. VAR result: BOOLEAN;
  4505. BEGIN
  4506. result := FALSE;
  4507. IF x = SyntaxTree.invalidExpression THEN
  4508. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4509. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4510. ELSE result := TRUE
  4511. END;
  4512. RETURN result
  4513. END CheckTypeDeclarationType;
  4514. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4515. VAR result: BOOLEAN;
  4516. BEGIN
  4517. result := FALSE;
  4518. IF x = SyntaxTree.invalidExpression THEN
  4519. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4520. result := TRUE;
  4521. value := x.resolved(SyntaxTree.IntegerValue).value;
  4522. ELSE
  4523. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4524. END;
  4525. RETURN result;
  4526. END CheckIntegerValue;
  4527. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4528. VAR result: BOOLEAN;
  4529. BEGIN
  4530. result := FALSE;
  4531. IF x = SyntaxTree.invalidExpression THEN
  4532. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4533. result := TRUE;
  4534. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4535. ELSE
  4536. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4537. END;
  4538. RETURN result;
  4539. END CheckStringValue;
  4540. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4541. BEGIN
  4542. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4543. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4544. ELSE
  4545. RETURN FALSE
  4546. END;
  4547. END IsUnsignedValue;
  4548. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4549. BEGIN
  4550. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4551. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4552. ELSE
  4553. RETURN FALSE
  4554. END
  4555. END IsAddressValue;
  4556. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4557. BEGIN
  4558. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4559. END IsAddressExpression;
  4560. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4561. BEGIN
  4562. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4563. END IsSizeExpression;
  4564. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4565. VAR result: BOOLEAN;
  4566. BEGIN
  4567. result := FALSE;
  4568. IF x = SyntaxTree.invalidExpression THEN
  4569. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4570. result := TRUE;
  4571. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4572. ELSE
  4573. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4574. END;
  4575. RETURN result;
  4576. END CheckEnumerationValue;
  4577. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4578. VAR result: BOOLEAN;
  4579. BEGIN
  4580. result := FALSE;
  4581. IF x = SyntaxTree.invalidExpression THEN
  4582. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4583. result := TRUE;
  4584. value := x.resolved(SyntaxTree.CharacterValue).value;
  4585. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4586. result := TRUE;
  4587. value := x.resolved(SyntaxTree.StringValue).value[0];
  4588. ELSE
  4589. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4590. END;
  4591. RETURN result;
  4592. END CheckCharacterValue;
  4593. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4594. VAR result: BOOLEAN;
  4595. BEGIN
  4596. result := FALSE;
  4597. IF x = SyntaxTree.invalidExpression THEN
  4598. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4599. value := x.resolved(SyntaxTree.IntegerValue).value;
  4600. IF (value > 0) OR includeZero & (value = 0) THEN
  4601. result := TRUE;
  4602. ELSE
  4603. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4604. END
  4605. ELSE
  4606. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4607. END;
  4608. RETURN result;
  4609. END CheckPositiveIntegerValue;
  4610. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4611. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4612. BEGIN
  4613. result := FALSE;
  4614. IF x = SyntaxTree.invalidExpression THEN
  4615. ELSE
  4616. type := x.type.resolved;
  4617. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4618. portType := type(SyntaxTree.PortType);
  4619. result := TRUE
  4620. ELSE
  4621. Error(x.position,Diagnostics.Invalid,"no port type");
  4622. END;
  4623. END;
  4624. RETURN result
  4625. END CheckPortType;
  4626. (* move to builtin procedure call statement ?
  4627. remove builtin procedure call designator ?
  4628. *)
  4629. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4630. VAR
  4631. numberActualParameters,numberFormalParameters: LONGINT;
  4632. formalParameter: SyntaxTree.Parameter;
  4633. actualParameter: SyntaxTree.Expression;
  4634. procedureType: SyntaxTree.ProcedureType;
  4635. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4636. inPort, outPort: SyntaxTree.PortType;
  4637. constructor: SyntaxTree.Procedure;
  4638. type0,type1,type2: SyntaxTree.Type;
  4639. type,base,parameterType: SyntaxTree.Type;
  4640. arrayType: SyntaxTree.ArrayType;
  4641. i,i0,i1: LONGINT;
  4642. r,r0,r1,im: LONGREAL;
  4643. c: CHAR;
  4644. id: LONGINT;
  4645. b: BOOLEAN;
  4646. first: LONGINT;
  4647. mathArrayType: SyntaxTree.MathArrayType;
  4648. customBuiltin: SyntaxTree.CustomBuiltin;
  4649. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4650. VAR resultB: BOOLEAN;
  4651. BEGIN
  4652. IF numberActualParameters < from THEN
  4653. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4654. result := SyntaxTree.invalidExpression;
  4655. resultB := FALSE;
  4656. ELSIF numberActualParameters > to THEN
  4657. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4658. result := SyntaxTree.invalidExpression;
  4659. resultB := FALSE;
  4660. ELSE
  4661. resultB := TRUE;
  4662. END;
  4663. RETURN resultB
  4664. END CheckArity;
  4665. BEGIN
  4666. type := NIL; result := NIL;
  4667. type0 := NIL; type1 := NIL; type2 := NIL;
  4668. numberActualParameters := actualParameters.Length();
  4669. IF numberActualParameters>0 THEN
  4670. parameter0 := actualParameters.GetExpression(0);
  4671. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4672. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4673. result := SyntaxTree.invalidExpression
  4674. END
  4675. END;
  4676. IF numberActualParameters >1 THEN
  4677. parameter1 := actualParameters.GetExpression(1);
  4678. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4679. ELSE
  4680. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4681. result := SyntaxTree.invalidExpression
  4682. END
  4683. END;
  4684. IF numberActualParameters >2 THEN
  4685. parameter2 := actualParameters.GetExpression(2);
  4686. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4687. ELSE
  4688. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4689. result := SyntaxTree.invalidExpression
  4690. END
  4691. END;
  4692. IF returnType # NIL THEN
  4693. id := Global.New;
  4694. result := NIL;
  4695. ELSE
  4696. id := builtin.id;
  4697. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4698. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4699. END;
  4700. END;
  4701. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4702. ELSIF result # NIL THEN type := result.type (* operator *)
  4703. ELSE
  4704. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4705. result(SyntaxTree.Designator).SetLeft(left);
  4706. IF returnType # NIL THEN
  4707. type := returnType;
  4708. END;
  4709. (* ---- ASSERT ----- *)
  4710. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4711. IF CheckBooleanType(parameter0) THEN
  4712. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4713. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4714. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4715. *)
  4716. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4717. Error(position,Diagnostics.Invalid,"assert failed");
  4718. END;
  4719. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4720. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4721. rules imposed by the architecture / current runtime
  4722. *)
  4723. END;
  4724. END;
  4725. (* ---- COPY ----- *)
  4726. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4727. IF~IsStringType(type0) THEN
  4728. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4729. END;
  4730. IF ~IsStringType(type1) THEN
  4731. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4732. ELSIF CheckVariable(parameter1) THEN
  4733. IF (type0 IS SyntaxTree.StringType) THEN
  4734. arrayType := type1(SyntaxTree.ArrayType);
  4735. IF arrayType.form = SyntaxTree.Static THEN
  4736. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4737. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4738. END;
  4739. END;
  4740. END;
  4741. END;
  4742. (* ---- INC, DEC----- *)
  4743. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4744. IF numberActualParameters = 1 THEN
  4745. parameter1 :=Global.NewIntegerValue(system,position,1);
  4746. actualParameters.AddExpression(parameter1);
  4747. END;
  4748. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4749. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4750. Error(position,Diagnostics.Invalid,"incompatible increment");
  4751. ELSE
  4752. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4753. actualParameters.SetExpression(1,parameter1);
  4754. END;
  4755. END;
  4756. (* ---- EXCL, INCL----- *)
  4757. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4758. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4759. IF IsIntegerValue(parameter1,i0) THEN
  4760. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4761. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4762. END;
  4763. END;
  4764. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4765. actualParameters.SetExpression(1,parameter1);
  4766. END;
  4767. (* ---- HALT, SYSTEM.HALT ----- *)
  4768. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4769. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4770. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4771. rules imposed by the architecture / current runtime
  4772. *)
  4773. END;
  4774. (* ---- WAIT ----- *)
  4775. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4776. IF CheckObjectType(parameter0) THEN
  4777. END;
  4778. (* ---- NEW ----- *)
  4779. ELSIF (id = Global.New) THEN
  4780. IF returnType # NIL THEN
  4781. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4782. ELSE
  4783. first := 1;
  4784. END;
  4785. IF CheckArity(first,Infinity) THEN
  4786. IF currentIsRealtime THEN
  4787. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4788. END;
  4789. (* check constructor *)
  4790. IF (first =0) OR CheckVariable(parameter0) THEN
  4791. IF type0 IS SyntaxTree.PointerType THEN
  4792. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4793. ELSIF type0 IS SyntaxTree.CellType THEN
  4794. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4795. ELSE
  4796. Error(position, Diagnostics.Invalid, "forbidden new on value type");
  4797. END;
  4798. IF type0 IS SyntaxTree.ArrayType THEN
  4799. arrayType := type0(SyntaxTree.ArrayType);
  4800. IF arrayType.form = SyntaxTree.Static THEN
  4801. i := first
  4802. ELSIF arrayType.form = SyntaxTree.Open THEN
  4803. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4804. ELSE HALT(100)
  4805. END;
  4806. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4807. i := first;
  4808. REPEAT
  4809. actualParameter := actualParameters.GetExpression(i);
  4810. IF CheckSizeType(actualParameter) THEN
  4811. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4812. actualParameters.SetExpression(i,actualParameter);
  4813. END;
  4814. INC(i);
  4815. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4816. END;
  4817. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4818. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4819. IF constructor = NIL THEN
  4820. IF CheckArity(first,first) THEN END;
  4821. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4822. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4823. ELSE
  4824. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4825. numberFormalParameters := procedureType.numberParameters;
  4826. IF numberActualParameters-first <= numberFormalParameters THEN
  4827. formalParameter := procedureType.firstParameter;
  4828. FOR i := first TO numberActualParameters-1 DO
  4829. actualParameter := actualParameters.GetExpression(i);
  4830. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4831. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4832. ELSE
  4833. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4834. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4835. END;
  4836. actualParameters.SetExpression(i,actualParameter);
  4837. END;
  4838. formalParameter := formalParameter.nextParameter;
  4839. END;
  4840. WHILE (formalParameter # NIL) DO
  4841. IF formalParameter.defaultValue # NIL THEN
  4842. actualParameters.AddExpression(formalParameter.defaultValue);
  4843. formalParameter := formalParameter.nextParameter
  4844. ELSE
  4845. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4846. formalParameter := NIL;
  4847. END;
  4848. END;
  4849. ELSE
  4850. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4851. END;
  4852. END;
  4853. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4854. mathArrayType := type0(SyntaxTree.MathArrayType);
  4855. IF mathArrayType.form = SyntaxTree.Static THEN
  4856. Error(position,Diagnostics.Invalid,"new on static array");
  4857. ELSE
  4858. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4859. i0 := first+1; i1 := Infinity;
  4860. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4861. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4862. i1 := i0;
  4863. ELSE HALT(100);
  4864. END;
  4865. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4866. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4867. base := ArrayBase(type0,MAX(LONGINT));
  4868. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4869. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4870. IF ~CompatibleTo(system,type0,parameterType) THEN
  4871. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4872. result := SyntaxTree.invalidExpression;
  4873. ELSE
  4874. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4875. END;
  4876. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4877. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4878. IF ~CompatibleTo(system,type1,parameterType) THEN
  4879. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4880. result := SyntaxTree.invalidExpression;
  4881. ELSE
  4882. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4883. END;
  4884. ELSE
  4885. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4886. i := first;
  4887. REPEAT
  4888. actualParameter := actualParameters.GetExpression(i);
  4889. IF CheckSizeType(actualParameter) THEN
  4890. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4891. actualParameters.SetExpression(i,actualParameter);
  4892. END;
  4893. INC(i);
  4894. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4895. END;
  4896. END;
  4897. END;
  4898. ELSIF type0 IS SyntaxTree.CellType THEN
  4899. IF ~(currentIsCellNet) THEN
  4900. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4901. ELSE
  4902. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4903. IF (constructor = NIL) & CheckArity(1,1) THEN
  4904. (* ok *)
  4905. ELSE
  4906. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4907. numberFormalParameters := procedureType.numberParameters;
  4908. DEC(numberActualParameters);
  4909. IF numberActualParameters <= numberFormalParameters THEN
  4910. formalParameter := procedureType.firstParameter;
  4911. FOR i := first TO numberActualParameters DO
  4912. actualParameter := actualParameters.GetExpression(i);
  4913. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4914. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4915. ELSE
  4916. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4917. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4918. END;
  4919. actualParameters.SetExpression(i,actualParameter);
  4920. END;
  4921. formalParameter := formalParameter.nextParameter;
  4922. END;
  4923. WHILE (formalParameter # NIL) DO
  4924. IF formalParameter.defaultValue # NIL THEN
  4925. actualParameters.AddExpression(formalParameter.defaultValue);
  4926. formalParameter := formalParameter.nextParameter
  4927. ELSE
  4928. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4929. formalParameter := NIL;
  4930. END;
  4931. END;
  4932. ELSE
  4933. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4934. END;
  4935. END;
  4936. END;
  4937. activeCellsStatement := TRUE;
  4938. ELSE
  4939. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4940. END;
  4941. END;
  4942. END;
  4943. (* ---- DISPOSE ----- *)
  4944. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4945. IF ~IsPointerType(parameter0.type) THEN
  4946. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4947. ELSIF ~IsDisposable(parameter0.type) THEN
  4948. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4949. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4950. END
  4951. (* ---- GETPROCEDURE ----- *)
  4952. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4953. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4954. IF CheckVariable(parameter2) THEN
  4955. IF ~GetProcedureAllowed(parameter2.type) THEN
  4956. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4957. END;
  4958. END;
  4959. END;
  4960. (* ---- ABS ----- *)
  4961. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4962. (* note: ABS on complex numbers is done using overloading *)
  4963. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4964. type := type0;
  4965. IF IsIntegerValue(parameter0,i0) THEN
  4966. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4967. type := Global.GetIntegerType(system,ABS(i0));
  4968. ELSIF IsRealValue(parameter0,r) THEN
  4969. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4970. END;
  4971. ELSE
  4972. type := SyntaxTree.invalidType;
  4973. END;
  4974. (* ---- ASH, ASR ----- *)
  4975. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4976. type := type0;
  4977. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4978. (*
  4979. ConvertOperands(parameter0,parameter1); (* same type *)
  4980. *)
  4981. type := parameter0.type;
  4982. IF IsIntegerValue(parameter0,i0) THEN
  4983. IF IsIntegerValue(parameter1,i1) THEN
  4984. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4985. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4986. result := ResolveExpression(result);
  4987. type := Global.GetIntegerType(system,i0);
  4988. END;
  4989. END;
  4990. IF type.resolved.sizeInBits < 32 THEN
  4991. type := system.longintType;
  4992. END;
  4993. (*!compatibility with release, remove when resolved
  4994. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4995. *)
  4996. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4997. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4998. actualParameters.SetExpression(0,parameter0);
  4999. actualParameters.SetExpression(1,parameter1);
  5000. END;
  5001. (* ---- CAP ----- *)
  5002. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5003. type := system.characterType;
  5004. IF CheckCharacterType (parameter0) THEN
  5005. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5006. actualParameters.SetExpression(0,parameter0);
  5007. IF IsCharacterValue(parameter0,c) THEN
  5008. IF (c <= "z") & (c >= "a") THEN
  5009. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5010. ELSE
  5011. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5012. END;
  5013. END;
  5014. END;
  5015. (* ---- CHR ----- *)
  5016. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5017. IF id = Global.Chr THEN
  5018. type := system.characterType
  5019. ELSE
  5020. type := system.characterType32
  5021. END;
  5022. IF CheckIntegerType(parameter0) THEN
  5023. IF IsIntegerValue(parameter0,i0) THEN
  5024. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5025. result := ResolveExpression(result);
  5026. ELSE
  5027. (*
  5028. result := NewConversion(parameter0.position,parameter0,type);
  5029. *)
  5030. END;
  5031. END
  5032. (* ---- ENTIER ----- *)
  5033. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5034. type := system.longintType;
  5035. IF CheckRealType(parameter0) THEN
  5036. IF IsRealValue(parameter0,r) THEN
  5037. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5038. type := Global.GetIntegerType(system,ENTIER(r));
  5039. END
  5040. END;
  5041. (* ---- ENTIERH ----- *)
  5042. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5043. type := system.hugeintType;
  5044. IF CheckRealType(parameter0) THEN
  5045. IF IsRealValue(parameter0,r) THEN
  5046. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5047. END
  5048. END;
  5049. (* ---- LEN ----- *)
  5050. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5051. type := system.longintType;
  5052. base := type0;
  5053. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5054. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5055. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5056. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5057. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5058. END;
  5059. type0 := base;
  5060. ELSE
  5061. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5062. type0 := parameter0.type.resolved;
  5063. actualParameters.SetExpression(0,parameter0);
  5064. base := type0;
  5065. END;
  5066. END;
  5067. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5068. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5069. IF i1 < 0 THEN
  5070. Error(position,Diagnostics.Invalid,"invalid dimension");
  5071. base := SyntaxTree.invalidType;
  5072. ELSE
  5073. base := ArrayBase(base,i1);
  5074. IF (base # NIL) & Indexable(base) THEN
  5075. ELSE
  5076. Error(position,Diagnostics.Invalid,"len on no array");
  5077. IF VerboseErrorMessage THEN
  5078. Printout.Info("base",base);
  5079. END;
  5080. base := SyntaxTree.invalidType;
  5081. END;
  5082. END;
  5083. IF numberActualParameters=2 THEN
  5084. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5085. actualParameters.SetExpression(1,parameter1);
  5086. ELSIF base IS SyntaxTree.MathArrayType THEN
  5087. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5088. END;
  5089. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5090. IF base IS SyntaxTree.ArrayType THEN
  5091. arrayType := base(SyntaxTree.ArrayType);
  5092. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5093. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5094. result := Global.NewIntegerValue(system,position,i);
  5095. type := result.type;(* arrayType.length.type;*)
  5096. ASSERT(type # NIL);
  5097. END;
  5098. ELSIF base IS SyntaxTree.MathArrayType THEN
  5099. mathArrayType := base(SyntaxTree.MathArrayType);
  5100. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5101. result := Global.NewIntegerValue(system,position,i);
  5102. type := result.type;
  5103. (*
  5104. type := mathArrayType.length.type;
  5105. *)
  5106. ASSERT(type # NIL);
  5107. END;
  5108. END;
  5109. END;
  5110. ELSE
  5111. type := system.longintType;
  5112. END;
  5113. (* ---- FIRST ---- *)
  5114. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5115. type := system.longintType;
  5116. IF CheckRangeType(parameter0) THEN END;
  5117. result.SetAssignable(parameter0.assignable)
  5118. (* ---- LAST ---- *)
  5119. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5120. type := system.longintType;
  5121. IF CheckRangeType(parameter0) THEN END;
  5122. result.SetAssignable(parameter0.assignable)
  5123. (* ---- STEP ---- *)
  5124. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5125. type := system.longintType;
  5126. IF CheckRangeType(parameter0) THEN END;
  5127. result.SetAssignable(parameter0.assignable)
  5128. (* ---- RE ---- *)
  5129. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5130. IF CheckNumberType(parameter0) THEN
  5131. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5132. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5133. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5134. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5135. type := parameter0.type
  5136. ELSE
  5137. type := system.realType
  5138. END
  5139. END;
  5140. result.SetAssignable(parameter0.assignable)
  5141. (* ---- IM ---- *)
  5142. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5143. IF CheckNumberType(parameter0) THEN
  5144. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5145. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5146. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5147. ELSE
  5148. type := system.realType;
  5149. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5150. END
  5151. END;
  5152. result.SetAssignable(parameter0.assignable)
  5153. (* ---- MAX ----- *)
  5154. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5155. IF numberActualParameters = 1 THEN
  5156. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5157. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5158. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5159. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5160. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5161. *)
  5162. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5163. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5164. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5165. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5166. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5167. END;
  5168. ELSE
  5169. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5170. END
  5171. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5172. ConvertOperands(parameter0,parameter1);
  5173. actualParameters.SetExpression(0,parameter0);
  5174. actualParameters.SetExpression(1,parameter1);
  5175. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5176. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5177. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5178. END;
  5179. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5180. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5181. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5182. END;
  5183. END;
  5184. type := parameter0.type;
  5185. ELSE type := SyntaxTree.invalidType;
  5186. END;
  5187. (* ---- MIN ----- *)
  5188. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5189. IF numberActualParameters = 1 THEN
  5190. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5191. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5192. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5193. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5194. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5195. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5196. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5197. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5198. END;
  5199. ELSE
  5200. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5201. END
  5202. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5203. ConvertOperands(parameter0,parameter1);
  5204. actualParameters.SetExpression(0,parameter0);
  5205. actualParameters.SetExpression(1,parameter1);
  5206. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5207. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5208. ELSE result.SetResolved(parameter1.resolved)
  5209. END;
  5210. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5211. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5212. ELSE result.SetResolved(parameter1.resolved)
  5213. END;
  5214. END;
  5215. type := parameter0.type;
  5216. ELSE type := SyntaxTree.invalidType;
  5217. END;
  5218. (* ---- ODD ----- *)
  5219. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5220. type := system.booleanType;
  5221. IF CheckIntegerType(parameter0) THEN
  5222. IF IsIntegerValue(parameter0,i0) THEN
  5223. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5224. type := system.booleanType;
  5225. END;
  5226. END;
  5227. (* ---- ORD ----- *)
  5228. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5229. IF id = Global.Ord THEN
  5230. type := system.integerType;
  5231. ELSE
  5232. type := system.longintType;
  5233. END;
  5234. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5235. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5236. actualParameters.SetExpression(0,parameter0);
  5237. (* IF CheckCharacterType(parameter0) THEN*)
  5238. IF IsCharacterValue(parameter0,c)THEN
  5239. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5240. type := Global.GetSignedIntegerType(system,ORD(c));
  5241. END;
  5242. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5243. END;
  5244. (* ---- SHORT ----- *)
  5245. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5246. type := type0;
  5247. IF IsSignedIntegerType(type) THEN
  5248. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5249. ELSIF type = system.integerType THEN type := system.shortintType
  5250. ELSIF type = system.longintType THEN type := system.integerType
  5251. ELSIF type = system.hugeintType THEN type:= system.longintType
  5252. ELSE
  5253. CASE type.sizeInBits OF
  5254. 16: type := Global.Integer8
  5255. |32: type := Global.Integer16
  5256. |64: type := Global.Integer32
  5257. END;
  5258. END;
  5259. ELSIF type IS SyntaxTree.FloatType THEN
  5260. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5261. ELSIF type = system.longrealType THEN type := system.realType
  5262. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5263. END;
  5264. ELSIF type IS SyntaxTree.ComplexType THEN
  5265. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5266. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5267. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5268. END;
  5269. ELSE
  5270. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5271. END;
  5272. IF (parameter0.resolved # NIL) THEN
  5273. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5274. IF parameter0 IS SyntaxTree.Value THEN
  5275. result.SetResolved(parameter0(SyntaxTree.Value));
  5276. END;
  5277. END;
  5278. (* ---- LONG ----- *)
  5279. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5280. type := type0;
  5281. IF IsSignedIntegerType(type) THEN
  5282. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5283. ELSIF type = system.longintType THEN type := system.hugeintType
  5284. ELSIF type = system.integerType THEN type := system.longintType
  5285. ELSIF type = system.shortintType THEN type := system.integerType
  5286. ELSE
  5287. CASE type.sizeInBits OF
  5288. 8: type := Global.Integer16
  5289. |16: type := Global.Integer32
  5290. |32: type := Global.Integer64
  5291. END;
  5292. END;
  5293. ELSIF type IS SyntaxTree.FloatType THEN
  5294. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5295. ELSIF type= system.realType THEN type := system.longrealType
  5296. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5297. END;
  5298. ELSIF type IS SyntaxTree.ComplexType THEN
  5299. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5300. ELSIF type = system.complexType THEN type := system.longcomplexType
  5301. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5302. END;
  5303. ELSE
  5304. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5305. END;
  5306. IF (parameter0.resolved # NIL) THEN
  5307. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5308. IF parameter0 IS SyntaxTree.Value THEN
  5309. result.SetResolved(parameter0(SyntaxTree.Value));
  5310. END;
  5311. END;
  5312. (* ---- SIZE OF ----- *)
  5313. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5314. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5315. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5316. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5317. type := system.integerType;
  5318. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5319. ELSE
  5320. (* for variables, system sizeof could represent the physically occupied size
  5321. determined via the type descriptor, implement that ? *)
  5322. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5323. END
  5324. (* ---- SYSTEM.TRACE -----*)
  5325. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5326. FOR i := 0 TO numberActualParameters-1 DO
  5327. parameter0 := actualParameters.GetExpression(i);
  5328. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5329. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5330. END;
  5331. END;
  5332. (* remaining issues can only be tested in backend *)
  5333. (* ---- ADDRESSOF----- *)
  5334. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5335. IF HasAddress(parameter0) THEN
  5336. type := system.addressType;
  5337. ELSE
  5338. type := SyntaxTree.invalidType;
  5339. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5340. END;
  5341. (* ---- BIT ----- *)
  5342. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5343. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5344. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5345. actualParameters.SetExpression(0,parameter0);
  5346. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5347. actualParameters.SetExpression(1,parameter1);
  5348. END;
  5349. type := system.booleanType;
  5350. (* ----- MSK ---- *)
  5351. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5352. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5353. ConvertOperands(parameter0,parameter1);
  5354. actualParameters.SetExpression(0,parameter0);
  5355. actualParameters.SetExpression(1,parameter1);
  5356. END;
  5357. type := parameter0.type;
  5358. (* ---- SYSTEM.GET64 ----- *)
  5359. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5360. IF CheckAddressType(parameter0) THEN
  5361. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5362. actualParameters.SetExpression(0,parameter0);
  5363. END;
  5364. type := system.hugeintType;
  5365. (* ---- SYSTEM.GET32 ----- *)
  5366. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5367. IF CheckAddressType(parameter0) THEN
  5368. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5369. actualParameters.SetExpression(0,parameter0);
  5370. END;
  5371. type := system.longintType;
  5372. (* ---- SYSTEM.GET16 ----- *)
  5373. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5374. IF CheckAddressType(parameter0) THEN
  5375. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5376. actualParameters.SetExpression(0,parameter0);
  5377. END;
  5378. type := system.integerType;
  5379. (* ---- SYSTEM.GET8 ----- *)
  5380. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5381. IF CheckAddressType(parameter0) THEN
  5382. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5383. actualParameters.SetExpression(0,parameter0);
  5384. END;
  5385. type := system.shortintType;
  5386. (* ---- SYSTEM.GetStackPointer ----- *)
  5387. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5388. type := system.addressType;
  5389. (* ---- SYSTEM.GetFramePointer ----- *)
  5390. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5391. type := system.addressType;
  5392. (* ---- SYSTEM.GetActivity ----- *)
  5393. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5394. type := system.objectType;
  5395. (* ---- SYSTEM.SetStackPointer ----- *)
  5396. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5397. IF CheckAddressType(parameter0) THEN
  5398. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5399. actualParameters.SetExpression(0,parameter0);
  5400. END;
  5401. (* ---- SYSTEM.SetFramePointer ----- *)
  5402. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5403. IF CheckAddressType(parameter0) THEN
  5404. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5405. actualParameters.SetExpression(0,parameter0);
  5406. END;
  5407. (* ---- SYSTEM.SetActivity ----- *)
  5408. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5409. IF CheckObjectType(parameter0) THEN
  5410. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5411. actualParameters.SetExpression(0,parameter0);
  5412. END;
  5413. (* ---- LSH, LSL, ROT, ROR ----- *)
  5414. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5415. type := type0;
  5416. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5417. actualParameters.SetExpression(1, parameter1);
  5418. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5419. IF id = Global.Lsh THEN
  5420. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5421. ELSIF id = Global.Rot THEN
  5422. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5423. ELSIF id = Global.Ror THEN
  5424. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5425. END;
  5426. END;
  5427. (* ---- SYSTEM.VAL ----- *)
  5428. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5429. IF CheckTypeDeclarationType(parameter0) THEN
  5430. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5431. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5432. result := SyntaxTree.invalidExpression;
  5433. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5434. ELSE
  5435. IF (parameter1.resolved # NIL) THEN
  5436. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5437. IF parameter0 IS SyntaxTree.Value THEN
  5438. result.SetResolved(parameter0(SyntaxTree.Value));
  5439. END;
  5440. END;
  5441. result.SetAssignable(parameter1.assignable);
  5442. END;
  5443. END;
  5444. (* ---- SYSTEM.GET ----- *)
  5445. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5446. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5447. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5448. actualParameters.SetExpression(0,parameter0);
  5449. END;
  5450. (* ---- SYSTEM.PUT ----- *)
  5451. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5452. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5453. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5454. actualParameters.SetExpression(0,parameter0);
  5455. END;
  5456. (* ---- SYSTEM.PUT64 ----- *)
  5457. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5458. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5459. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5460. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5461. actualParameters.SetExpression(0,parameter0);
  5462. actualParameters.SetExpression(1,parameter1);
  5463. END;
  5464. (* ---- SYSTEM.PUT32 ----- *)
  5465. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5466. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5467. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5468. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5469. actualParameters.SetExpression(0,parameter0);
  5470. actualParameters.SetExpression(1,parameter1);
  5471. END;
  5472. (* ---- SYSTEM.PUT16 ----- *)
  5473. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5474. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5475. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5476. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5477. actualParameters.SetExpression(0,parameter0);
  5478. actualParameters.SetExpression(1,parameter1);
  5479. END;
  5480. (* ---- SYSTEM.PUT8 ----- *)
  5481. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5482. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5483. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5484. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5485. actualParameters.SetExpression(0,parameter0);
  5486. actualParameters.SetExpression(1,parameter1);
  5487. END;
  5488. (* ---- SYSTEM.MOVE ----- *)
  5489. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5490. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5491. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5492. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5493. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5494. actualParameters.SetExpression(0,parameter0);
  5495. actualParameters.SetExpression(1,parameter1);
  5496. actualParameters.SetExpression(2,parameter2);
  5497. END;
  5498. (* ---- SYSTEM.NEW ----- *)
  5499. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5500. IF ~IsPointerType(parameter0.type) THEN
  5501. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5502. ELSIF CheckSizeType(parameter1) THEN
  5503. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5504. actualParameters.SetExpression(1,parameter1);
  5505. END;
  5506. (* ----SYSTEM.REF ---- *)
  5507. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5508. type := system.addressType
  5509. (* ---- INCR ----- *)
  5510. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5511. type := system.sizeType;
  5512. base := type0;
  5513. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5514. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5515. IF i1 < 0 THEN
  5516. Error(position,Diagnostics.Invalid,"invalid dimension");
  5517. base := SyntaxTree.invalidType;
  5518. ELSE
  5519. base := ArrayBase(base,i1);
  5520. IF (base # NIL) & Indexable(base) THEN
  5521. ELSE
  5522. Error(position,Diagnostics.Invalid,"len on no array");
  5523. IF VerboseErrorMessage THEN
  5524. Printout.Info("base",base);
  5525. END;
  5526. base := SyntaxTree.invalidType;
  5527. END;
  5528. END;
  5529. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5530. actualParameters.SetExpression(1,parameter1);
  5531. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5532. mathArrayType := base(SyntaxTree.MathArrayType);
  5533. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5534. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5535. type := system.longintType;
  5536. END;
  5537. END;
  5538. ELSE
  5539. type := system.longintType;
  5540. END;
  5541. (* ---- SUM ----- *)
  5542. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5543. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5544. (* ---- ALL ----- *)
  5545. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5546. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5547. (* ---- DIM ----- *)
  5548. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5549. type := system.sizeType;
  5550. IF type0 IS SyntaxTree.MathArrayType THEN
  5551. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5552. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5553. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5554. END;
  5555. ELSE
  5556. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5557. END;
  5558. (* ---- CAS ----- *)
  5559. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5560. IF type0.IsComposite () THEN
  5561. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5562. result := SyntaxTree.invalidExpression;
  5563. ELSIF ~IsVariable (parameter0) THEN
  5564. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5565. result := SyntaxTree.invalidExpression;
  5566. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5567. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5568. result := SyntaxTree.invalidExpression;
  5569. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5570. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5571. result := SyntaxTree.invalidExpression;
  5572. ELSE
  5573. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5574. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5575. type := type0;
  5576. END;
  5577. (* ---- RESHAPE ----- *)
  5578. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5579. IF type0 IS SyntaxTree.MathArrayType THEN
  5580. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5581. base := ArrayBase(type0,MAX(LONGINT));
  5582. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5583. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5584. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5585. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5586. IF ~CompatibleTo(system,type0,parameterType) THEN
  5587. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5588. result := SyntaxTree.invalidExpression;
  5589. ELSE
  5590. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5591. END;
  5592. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5593. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5594. IF ~CompatibleTo(system,type1,parameterType) THEN
  5595. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5596. result := SyntaxTree.invalidExpression;
  5597. ELSE
  5598. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5599. END;
  5600. ELSE
  5601. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5602. result := SyntaxTree.invalidExpression;
  5603. END;
  5604. (* ---- SYSTEM.TYPECODE ----- *)
  5605. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5606. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5607. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5608. type := type.resolved;
  5609. IF type IS SyntaxTree.PointerType THEN
  5610. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5611. END;
  5612. IF ~(type IS SyntaxTree.RecordType) THEN
  5613. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5614. END;
  5615. ELSE
  5616. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5617. END;
  5618. type := system.addressType;
  5619. (* -------- FLT --------- *)
  5620. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5621. type := system.realType;
  5622. IF IsRealValue(parameter0, r) THEN
  5623. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5624. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5625. i0 := i; i := ABS(i);
  5626. IF i # 0 THEN
  5627. i1 := 23;
  5628. IF i >= 2*800000H THEN
  5629. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5630. ELSIF i < 800000H THEN
  5631. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5632. END;
  5633. i := (i1 + 127)*800000H - 800000H + i;
  5634. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5635. END;
  5636. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5637. END;
  5638. (* ------- CONNECT -------*)
  5639. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5640. (*IF ~(currentIsCellNet) THEN
  5641. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5642. END;*)
  5643. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5644. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5645. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5646. END;
  5647. IF numberActualParameters = 3 THEN
  5648. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5649. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5650. END;
  5651. *)
  5652. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5653. actualParameters.SetExpression(2,parameter2);
  5654. END;
  5655. activeCellsStatement := TRUE;
  5656. (* ---------- DELEGATE --------*)
  5657. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5658. (*
  5659. IF ~(currentIsCellNet) THEN
  5660. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5661. END;
  5662. *)
  5663. IF ~CheckPortType(parameter1, inPort) THEN
  5664. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5665. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5666. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5667. ELSIF (outPort.direction # inPort.direction) THEN
  5668. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5669. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5670. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5671. END;
  5672. activeCellsStatement := TRUE;
  5673. (* --------- RECEIVE ---------*)
  5674. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5675. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5676. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5677. IF inPort.direction # SyntaxTree.InPort THEN
  5678. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5679. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5680. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5681. END;
  5682. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5683. IF ~SameType(parameter2.type, system.integerType) THEN
  5684. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5685. END;
  5686. END;
  5687. END;
  5688. (* --------- SEND ---------*)
  5689. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5690. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5691. IF CheckPortType(parameter0,outPort) THEN
  5692. IF outPort.direction # SyntaxTree.OutPort THEN
  5693. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5694. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5695. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5696. ELSE
  5697. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5698. actualParameters.SetExpression(1,parameter1);
  5699. END;
  5700. END;
  5701. (* ------- custom builtins ----- *)
  5702. ELSIF id = Global.systemSpecial THEN
  5703. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5704. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5705. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5706. type := procedureType.returnType;
  5707. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5708. (* go through all formal parameters *)
  5709. formalParameter := procedureType.firstParameter;
  5710. FOR i := 0 TO actualParameters.Length() - 1 DO
  5711. actualParameter := actualParameters.GetExpression(i);
  5712. IF actualParameter = SyntaxTree.invalidExpression THEN
  5713. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5714. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5715. ELSE
  5716. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5717. END;
  5718. actualParameters.SetExpression(i, actualParameter);
  5719. formalParameter := formalParameter.nextParameter
  5720. END
  5721. END
  5722. ELSE
  5723. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5724. result := SyntaxTree.invalidExpression;
  5725. END;
  5726. END;
  5727. IF result # SyntaxTree.invalidExpression THEN
  5728. type := ResolveType(type);
  5729. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5730. result.SetType(type);
  5731. END;
  5732. RETURN result
  5733. END NewBuiltinCallDesignator;
  5734. (** return type guard designator left(type)
  5735. - check if type can be extended (i.e. is no static record)
  5736. - check if type is a type extension of left.type
  5737. - returns new type guard designator
  5738. returns invalidDesignator = invalidExpression if error
  5739. **)
  5740. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5741. VAR result: SyntaxTree.Designator;
  5742. BEGIN
  5743. result := SyntaxTree.invalidDesignator;
  5744. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5745. Error(position,Diagnostics.Invalid,"no type extension of type");
  5746. IF VerboseErrorMessage THEN
  5747. Printout.Info("left",left);
  5748. Printout.Info("type",type);
  5749. END;
  5750. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5751. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5752. ELSIF IsUnsafePointer(left.type) THEN
  5753. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5754. ELSE
  5755. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5756. result.SetType(type);
  5757. result.SetAssignable(left.assignable);
  5758. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5759. END;
  5760. RETURN result
  5761. END NewTypeGuardDesignator;
  5762. (** check and resolve parameter designator left(expression list)
  5763. - check expression list
  5764. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5765. - elsif left is a procedure type then
  5766. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5767. - else return is a procedure call then return ProcedureCallDesignator
  5768. returns invalidDesignator = invalidExpression if error
  5769. **)
  5770. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5771. VAR
  5772. parameters: SyntaxTree.ExpressionList;
  5773. left: SyntaxTree.Designator;
  5774. result,expression: SyntaxTree.Expression;
  5775. typeDeclaration: SyntaxTree.TypeDeclaration;
  5776. type, expressionType: SyntaxTree.Type;
  5777. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5778. BEGIN
  5779. type := type.resolved;
  5780. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5781. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5782. END;
  5783. RETURN type
  5784. END BaseType;
  5785. BEGIN
  5786. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5787. result := SyntaxTree.invalidDesignator;
  5788. left := ResolveDesignator(designator.left);
  5789. IF left # SyntaxTree.invalidDesignator THEN
  5790. parameters := designator.parameters;
  5791. IF ExpressionList(parameters) THEN
  5792. IF (left.type = NIL) THEN
  5793. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5794. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5795. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5796. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5797. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5798. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5799. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5800. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5801. ELSE
  5802. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5803. END
  5804. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5805. expression := parameters.GetExpression(0);
  5806. type := typeDeclaration.declaredType.resolved;
  5807. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5808. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5809. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5810. OR (expressionType IS SyntaxTree.EnumerationType)
  5811. ) THEN
  5812. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5813. ELSE
  5814. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5815. END;
  5816. ELSE
  5817. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5818. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5819. result := SyntaxTree.invalidDesignator;
  5820. END;
  5821. ELSE
  5822. result := SyntaxTree.invalidDesignator
  5823. END;
  5824. END;
  5825. resolvedExpression := result;
  5826. END VisitParameterDesignator;
  5827. (** check dereference designator left^
  5828. - check if left is pointer type or left is object type
  5829. - return new dereference designator with type = left.baseType.type (if appropriate)
  5830. with error handling
  5831. returns invalidDesignator = invalidExpression if error
  5832. **)
  5833. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5834. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5835. BEGIN
  5836. result := SyntaxTree.invalidDesignator;
  5837. type := left.type;
  5838. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5839. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5840. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5841. result.SetAssignable(TRUE);
  5842. result.SetType(type);
  5843. result.SetHidden(left.isHidden);
  5844. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5845. type := type.resolved;
  5846. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5847. result.SetAssignable(TRUE);
  5848. result.SetType(type);
  5849. result.SetHidden(left.isHidden);
  5850. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5851. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5852. result.SetAssignable(TRUE);
  5853. result.SetType(type);
  5854. result.SetHidden(left.isHidden);
  5855. ELSE
  5856. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5857. IF VerboseErrorMessage THEN
  5858. Printout.Info("pointer", type);
  5859. Printout.Info("scope", currentScope);
  5860. END;
  5861. END;
  5862. RETURN result
  5863. END NewDereferenceDesignator;
  5864. (** check supercall designator left^
  5865. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5866. - return new supercall designator with type = left.type
  5867. with error handling
  5868. **)
  5869. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5870. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5871. objectScope: SyntaxTree.Scope;
  5872. BEGIN
  5873. result := SyntaxTree.invalidDesignator;
  5874. IF left = SyntaxTree.invalidDesignator THEN
  5875. (* error already handled *)
  5876. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5877. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5878. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5879. IF symbol IS SyntaxTree.Procedure THEN
  5880. procedure := symbol(SyntaxTree.Procedure);
  5881. objectScope := currentScope;
  5882. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5883. objectScope := objectScope.outerScope;
  5884. END;
  5885. IF (left.left = NIL) OR ~
  5886. (
  5887. (left.left IS SyntaxTree.SelfDesignator) OR
  5888. (left.left IS SyntaxTree.DereferenceDesignator)
  5889. & (left.left(SyntaxTree.Designator).left # NIL)
  5890. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5891. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5892. IF VerboseErrorMessage THEN
  5893. Printout.Info("left.left",left.left);
  5894. END;
  5895. ELSIF procedure.super # NIL THEN
  5896. result := SyntaxTree.NewSupercallDesignator(position,left);
  5897. result.SetType(left.type.resolved)
  5898. ELSE
  5899. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5900. END;
  5901. ELSE
  5902. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5903. END;
  5904. ELSE
  5905. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5906. END;
  5907. RETURN result
  5908. END NewSupercallDesignator;
  5909. (** check and semantically resolve arrow designator left^
  5910. - if left is procedure type -> result := SupercallDesignator
  5911. - else result := DereferenceDesignator
  5912. returns result via global variable resolvedExpression
  5913. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5914. **)
  5915. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5916. VAR left: SyntaxTree.Designator;
  5917. BEGIN
  5918. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5919. left := ResolveDesignator(arrowDesignator.left);
  5920. IF left # NIL THEN
  5921. IF (left.type = NIL) THEN
  5922. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5923. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5924. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5925. ELSE
  5926. IF IsPointerToObject(left.type) THEN
  5927. Warning(arrowDesignator.position, "forbidden dereference on object");
  5928. END;
  5929. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5930. END
  5931. END
  5932. END VisitArrowDesignator;
  5933. (** check and return expression
  5934. - if expression has no type then resolve expression
  5935. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5936. - return result
  5937. **)
  5938. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5939. VAR result,prev: SyntaxTree.Expression;
  5940. BEGIN
  5941. IF expression = NIL THEN result := NIL
  5942. ELSIF (expression.type = NIL) THEN
  5943. prev := resolvedExpression;
  5944. resolvedExpression := SyntaxTree.invalidExpression;
  5945. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5946. expression.SetType(SyntaxTree.invalidType);
  5947. END;
  5948. expression.Accept(SELF);
  5949. result := resolvedExpression;
  5950. IF currentIsRealtime THEN
  5951. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5952. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5953. END;
  5954. END;
  5955. (* designator modifiers for backends if they support it ...*)
  5956. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5957. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5958. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5959. END;
  5960. resolvedExpression := prev
  5961. ELSE
  5962. result := expression
  5963. END;
  5964. RETURN result
  5965. END ResolveExpression;
  5966. (**
  5967. check expression to be constant expression
  5968. - resolve expression
  5969. - if valid then check that of value type
  5970. report error and return invalidExpression if anything fails
  5971. **)
  5972. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5973. VAR position: LONGINT;
  5974. BEGIN
  5975. position := expression.position;
  5976. expression := ResolveExpression(expression);
  5977. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5978. ELSIF (expression.resolved = NIL) THEN
  5979. Error(position,Diagnostics.Invalid,"expression is not constant");
  5980. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5981. expression := SyntaxTree.invalidExpression;
  5982. END;
  5983. RETURN expression
  5984. END ConstantExpression;
  5985. (** check expression to be constant integer
  5986. - resolve expresssion
  5987. - if valid then check that of integer value type
  5988. report error and return invalidExpression if anything fails
  5989. **)
  5990. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5991. VAR position: LONGINT;
  5992. BEGIN
  5993. position := expression.position;
  5994. expression := ResolveExpression(expression);
  5995. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5996. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5997. expression := SyntaxTree.invalidExpression;
  5998. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5999. END;
  6000. RETURN expression
  6001. END ConstantInteger;
  6002. (** check expression as positive (>=0) constant integer
  6003. - resolve expression
  6004. - if valid then check that integer value
  6005. - if integer value then check that value >= 0
  6006. report error and return invalidExpression if anything fails
  6007. **)
  6008. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6009. VAR position: LONGINT;
  6010. BEGIN
  6011. position := expression.position;
  6012. expression := ConstantExpression(expression);
  6013. IF expression = SyntaxTree.invalidExpression THEN
  6014. (* error already reported *)
  6015. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6016. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  6017. expression := SyntaxTree.invalidExpression
  6018. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6019. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  6020. END;
  6021. RETURN expression
  6022. END ConstantIntegerGeq0;
  6023. (** check expression as condition
  6024. - resolve expression
  6025. - if valid expression then check that result type is boolean
  6026. report error and return invalidExpression if anything fails
  6027. **)
  6028. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6029. VAR position: LONGINT;
  6030. BEGIN
  6031. position := expression.position;
  6032. expression := ResolveExpression(expression);
  6033. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6034. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6035. expression := SyntaxTree.invalidExpression;
  6036. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6037. END;
  6038. RETURN expression
  6039. END ResolveCondition;
  6040. (*** symbols ***)
  6041. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6042. BEGIN
  6043. x.Accept(SELF);
  6044. END ResolveSymbol;
  6045. (** check a symbol
  6046. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6047. **)
  6048. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6049. VAR scope: SyntaxTree.Scope;
  6050. BEGIN
  6051. (* visibility *)
  6052. scope := symbol.scope;
  6053. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6054. scope := scope.outerScope;
  6055. END;
  6056. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6057. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6058. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6059. IF VerboseErrorMessage THEN
  6060. Printout.Info("symbol",symbol);
  6061. END;
  6062. END;
  6063. END;
  6064. END CheckSymbolVisibility;
  6065. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6066. If node is currently being resolved then emit a cyclic definition error.
  6067. Return TRUE only if node is fully resolved.
  6068. **)
  6069. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6070. VAR result: BOOLEAN;
  6071. BEGIN
  6072. IF SyntaxTree.Resolved IN x.state THEN
  6073. result := FALSE
  6074. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6075. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6076. result := FALSE;
  6077. ELSE
  6078. result := TRUE;
  6079. x.SetState(SyntaxTree.BeingResolved)
  6080. END;
  6081. RETURN result
  6082. END SymbolNeedsResolution;
  6083. (** check and resolve a type declaration symbol = Type
  6084. - set type to declaration type
  6085. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6086. This is so because the type declaration itself does not have a type but it only stands for a type.
  6087. In the implementation of the compiler this made a lot much easier.
  6088. - resolve and set declared type
  6089. - check symbol
  6090. **)
  6091. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6092. VAR prevScope: SyntaxTree.Scope;
  6093. BEGIN
  6094. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6095. IF SymbolNeedsResolution(typeDeclaration) THEN
  6096. prevScope := currentScope;
  6097. currentScope := typeDeclaration.scope;
  6098. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6099. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6100. CheckSymbolVisibility(typeDeclaration);
  6101. typeDeclaration.SetState(SyntaxTree.Resolved);
  6102. currentScope := prevScope;
  6103. END;
  6104. END VisitTypeDeclaration;
  6105. (** check and resolve a constant declaration symbol = (constant) expression
  6106. - check expression
  6107. - set type and value
  6108. - check symbol
  6109. **)
  6110. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6111. VAR
  6112. expression: SyntaxTree.Expression;
  6113. type: SyntaxTree.Type;
  6114. name: Basic.SegmentedName;
  6115. replacement: Replacement;
  6116. BEGIN
  6117. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6118. IF SymbolNeedsResolution(constant) THEN
  6119. expression := constant.value;
  6120. IF replacements # NIL THEN
  6121. Global.GetSymbolSegmentedName(constant, name);
  6122. replacement := replacements;
  6123. WHILE (replacement # NIL) & (replacement.name # name) DO
  6124. replacement := replacement.next;
  6125. END;
  6126. IF replacement # NIL THEN
  6127. InfoSS(constant.position, "replacing constant", constant.name);
  6128. (*
  6129. NEW(stringReader, Strings.Length(replacement.string^));
  6130. stringReader.Set(replacement.string^);
  6131. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6132. NEW(parser, scanner, diagnostics);
  6133. expression := parser.Expression();
  6134. *)
  6135. expression := replacement.expression;
  6136. replacement.used := TRUE;
  6137. END;
  6138. END;
  6139. constant.SetType(SyntaxTree.invalidType);
  6140. expression := ConstantExpression(expression);
  6141. ASSERT(expression.type # NIL);
  6142. type := expression.type.resolved;
  6143. constant.SetType(type);
  6144. constant.SetValue(expression);
  6145. CheckSymbolVisibility(constant);
  6146. constant.SetState(SyntaxTree.Resolved);
  6147. END;
  6148. END VisitConstant;
  6149. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6150. VAR procedureAlignment: LONGINT;
  6151. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6152. (* least common multiple *)
  6153. VAR a,b: LONGINT;
  6154. BEGIN
  6155. a := a0; b := b0;
  6156. WHILE (a # b) DO
  6157. IF a < b THEN a := a+a0
  6158. ELSE b := b + b0
  6159. END;
  6160. END;
  6161. RETURN a
  6162. END LCM;
  6163. BEGIN
  6164. IF alignment > 1 THEN
  6165. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6166. IF (procedureAlignment > 1) THEN
  6167. alignment := LCM(alignment, procedureAlignment);
  6168. END;
  6169. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6170. END;
  6171. END AdaptStackAlignment;
  6172. (** check and resolve a variable / field
  6173. - check and set type
  6174. - negative check on open array type
  6175. - check symbol
  6176. **)
  6177. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6178. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6179. BEGIN
  6180. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6181. IF SymbolNeedsResolution(variable) THEN
  6182. modifiers := variable.modifiers;
  6183. (*
  6184. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6185. variable.AddFlags(flags);
  6186. *)
  6187. variable.SetType(ResolveType(variable.type));
  6188. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6189. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6190. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6191. END;
  6192. END;
  6193. CheckSymbolVisibility(variable);
  6194. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6195. variable.SetUntraced(TRUE);
  6196. IF ~ContainsPointer(variable.type) THEN
  6197. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6198. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6199. END;
  6200. END;
  6201. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6202. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6203. IF ~PowerOf2(value) THEN
  6204. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6205. ELSE
  6206. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6207. END;
  6208. END;
  6209. variable.SetAlignment(FALSE,value);
  6210. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6211. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6212. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6213. END;
  6214. variable.SetAlignment(TRUE, value);
  6215. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6216. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6217. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6218. END;
  6219. variable.SetFictive(value);
  6220. variable.SetOffset(value*system.dataUnit);
  6221. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6222. END;
  6223. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6224. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6225. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6226. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6227. END;
  6228. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6229. IF variable.initializer # NIL THEN
  6230. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6231. END;
  6232. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6233. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6234. pointerType.SetPointerBase(variable.type);
  6235. pointerType.SetHidden(TRUE);
  6236. variable.SetType(ResolveType(pointerType));
  6237. END;
  6238. variable.SetState(SyntaxTree.Resolved);
  6239. END;
  6240. END VisitVariable;
  6241. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6242. BEGIN
  6243. VisitVariable(property)
  6244. END VisitProperty;
  6245. (** check and resolve a (procedure) parameter
  6246. - check and set type
  6247. - check symbol
  6248. - check parameter kind and set read-only flags if appropriate
  6249. **)
  6250. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6251. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6252. BEGIN
  6253. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6254. IF SymbolNeedsResolution(parameter) THEN
  6255. modifiers := parameter.modifiers;
  6256. parameter.SetType(ResolveType(parameter.type));
  6257. ASSERT(parameter.type.resolved # NIL);
  6258. CheckSymbolVisibility(parameter);
  6259. IF parameter.defaultValue # NIL THEN
  6260. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6261. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6262. ELSE
  6263. expression := ConstantExpression(parameter.defaultValue);
  6264. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6265. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6266. parameter.SetDefaultValue(expression);
  6267. END;
  6268. END;
  6269. END;
  6270. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6271. parameter.SetUntraced(TRUE);
  6272. IF ~ContainsPointer(parameter.type) THEN
  6273. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6274. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6275. END;
  6276. END;
  6277. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6278. parameter.SetMoveable(TRUE);
  6279. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6280. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6281. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6282. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6283. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6284. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6285. END;
  6286. END;
  6287. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6288. parameter.SetState(SyntaxTree.Resolved);
  6289. END;
  6290. END VisitParameter;
  6291. (** check and resolve a procedure (with declaration and implementation scope)
  6292. - check the procedure type
  6293. - check if method (i.e. in record scope), if so then
  6294. - check if (unique) constructor
  6295. - check if (unique) finalizer
  6296. - check if super method available, if so then check signature
  6297. - of not in record scope then negative check on constructor flag
  6298. - of not in record scope then negative check on finalizer flag
  6299. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6300. - check procedure symbol
  6301. **)
  6302. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6303. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6304. procedureType: SyntaxTree.ProcedureType;
  6305. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6306. qualifiedType: SyntaxTree.QualifiedType;
  6307. value: LONGINT;
  6308. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6309. position: LONGINT;
  6310. BEGIN
  6311. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6312. IF IsOberonInline(procedure) THEN
  6313. IF SyntaxTree.Public * procedure.access # {} THEN
  6314. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6315. END;
  6316. procedure.SetInline(FALSE);
  6317. procedure.SetOberonInline(TRUE);
  6318. END;
  6319. IF SymbolNeedsResolution(procedure) THEN
  6320. recentIsRealtime := currentIsRealtime;
  6321. recentIsBodyProcedure := currentIsBodyProcedure;
  6322. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6323. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6324. modifiers := procedureType.modifiers;
  6325. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6326. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6327. IF useDarwinCCalls THEN (*fld*)
  6328. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6329. ELSE
  6330. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6331. END
  6332. END;
  6333. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6334. procedureType.SetInterrupt(TRUE);
  6335. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6336. END;
  6337. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6338. procedureType.SetNoReturn(TRUE);
  6339. END;
  6340. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6341. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6342. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6343. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6344. END;
  6345. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6346. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6347. END;
  6348. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6349. IF ~PowerOf2(value) THEN
  6350. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6351. ELSE
  6352. procedureType.SetStackAlignment(value)
  6353. END;
  6354. END;
  6355. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6356. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6357. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6358. END;
  6359. CheckModifiers(modifiers, TRUE);
  6360. modifiers := procedureType.returnTypeModifiers;
  6361. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6362. CheckModifiers(modifiers, TRUE);
  6363. FixProcedureType(procedureType);
  6364. currentIsRealtime := procedureType.isRealtime;
  6365. currentIsBodyProcedure := procedure.isBodyProcedure;
  6366. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6367. THEN
  6368. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6369. END;
  6370. CheckSymbolVisibility(procedure);
  6371. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6372. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6373. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6374. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6375. END;
  6376. END;
  6377. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6378. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6379. procedureType.SetDelegate(TRUE);
  6380. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6381. IF record.pointerType.typeDeclaration = NIL THEN
  6382. selfParameter.SetType(record.pointerType);
  6383. ELSE
  6384. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6385. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6386. qualifiedType.SetResolved(record.pointerType);
  6387. selfParameter.SetType(qualifiedType);
  6388. END;
  6389. selfParameter.SetAccess(SyntaxTree.Hidden);
  6390. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6391. IF procedure.isConstructor THEN
  6392. (*! constructor is always visible, compatibility to paco
  6393. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6394. *)
  6395. procedure.MarkUsed;
  6396. IF procedureType.returnType # NIL THEN
  6397. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6398. END;
  6399. proc := procedure.scope.firstProcedure;
  6400. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6401. proc := proc.nextProcedure;
  6402. END;
  6403. IF proc # NIL THEN
  6404. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6405. ELSE
  6406. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6407. END;
  6408. END;
  6409. IF procedure.isFinalizer THEN
  6410. procedure.MarkUsed;
  6411. IF procedureType.returnType # NIL THEN
  6412. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6413. END;
  6414. IF procedureType.numberParameters # 0 THEN
  6415. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6416. END;
  6417. proc := procedure.scope.firstProcedure;
  6418. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6419. proc := proc.nextProcedure;
  6420. END;
  6421. IF proc # NIL THEN
  6422. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6423. ELSE
  6424. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6425. END;
  6426. END;
  6427. super := FindSuperProcedure(record.recordScope, procedure);
  6428. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6429. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6430. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6431. END;
  6432. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6433. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6434. END;
  6435. IF super.isFinal THEN
  6436. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6437. END;
  6438. procedure.SetSuper(super);
  6439. super.SetOverwritten(TRUE);
  6440. procedure.SetAccess(procedure.access+super.access);
  6441. procedure.MarkUsed;
  6442. END;
  6443. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6444. THEN
  6445. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6446. END;
  6447. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6448. IF cellsAreObjects THEN
  6449. procedureType.SetDelegate(TRUE);
  6450. END;
  6451. IF procedure.isConstructor THEN
  6452. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6453. END;
  6454. ELSIF procedure.isConstructor THEN
  6455. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6456. END;
  6457. Declarations(procedure.procedureScope, FALSE);
  6458. (* body resolution part done as late fix of the procedure type *)
  6459. procedure.SetState(SyntaxTree.Resolved);
  6460. currentIsRealtime := recentIsRealtime;
  6461. currentIsBodyProcedure := recentIsBodyProcedure;
  6462. END;
  6463. END VisitProcedure;
  6464. (**
  6465. a builtin procedure is a global item that may not be modified locally
  6466. instead the resolving of builtin procedure calls are done in the esignator
  6467. **)
  6468. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6469. VAR type: SyntaxTree.Type;
  6470. BEGIN
  6471. type := ResolveType(builtinProcedure.type);
  6472. END VisitBuiltin;
  6473. (* nopov *)
  6474. (** check and resolve operator
  6475. - operators are first checked as procedures
  6476. - then additional operator-specific checks are done
  6477. - note that only module-scope operators are checked here
  6478. (operators in a record scope are only allowed in the context of
  6479. array-structured object types and checked in 'ResolveArrayStructure')
  6480. - also note that inter-operator conformity is not checked here
  6481. **)
  6482. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6483. VAR
  6484. procedureType: SyntaxTree.ProcedureType;
  6485. leftType, rightType: SyntaxTree.Type;
  6486. identifierNumber, position: LONGINT;
  6487. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6488. modifiers: SyntaxTree.Modifier;
  6489. (** whether a type is locally defined in the current module scope
  6490. for arrays, the base type must be locally defined **)
  6491. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6492. BEGIN
  6493. IF type = NIL THEN
  6494. RETURN FALSE
  6495. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6496. RETURN TRUE
  6497. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6498. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6499. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6500. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6501. ELSE
  6502. RETURN FALSE
  6503. END
  6504. END IsLocallyDefined;
  6505. BEGIN
  6506. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6507. procedureType := operator.type(SyntaxTree.ProcedureType);
  6508. modifiers := procedureType.modifiers;
  6509. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6510. CheckModifiers(modifiers, TRUE);
  6511. VisitProcedure(operator);
  6512. IF operator.scope IS SyntaxTree.RecordScope THEN
  6513. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6514. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6515. IF identifierNumber = -1 THEN
  6516. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6517. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6518. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6519. ELSE
  6520. IF procedureType.numberParameters < 1 THEN
  6521. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6522. ELSIF procedureType.numberParameters > 2 THEN
  6523. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6524. ELSE
  6525. (* determine operand types *)
  6526. leftType := procedureType.firstParameter.type;
  6527. IF procedureType.numberParameters > 1 THEN
  6528. rightType := procedureType.firstParameter.nextParameter.type
  6529. ELSE
  6530. rightType := NIL
  6531. END;
  6532. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6533. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6534. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6535. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6536. END
  6537. END;
  6538. (* TODO: refine the checks, think about how restrictive the checks should be
  6539. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6540. They might be used for intersection, union, complement of custom object types *)
  6541. (* defaults *)
  6542. hasReturnType := TRUE;
  6543. mustBeUnary := FALSE;
  6544. mustBeBinary := FALSE;
  6545. mustReturnBoolean := FALSE;
  6546. mustReturnInteger := FALSE;
  6547. mustHaveEquitypedOperands := FALSE;
  6548. (* operator-specific exceptions *)
  6549. CASE identifierNumber OF
  6550. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6551. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6552. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6553. mustBeBinary := TRUE
  6554. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6555. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6556. | Scanner.Times: mustBeBinary := TRUE
  6557. | Scanner.TimesTimes: mustBeBinary := TRUE
  6558. | Scanner.DotTimes: mustBeBinary := TRUE
  6559. | Scanner.PlusTimes: mustBeBinary := TRUE
  6560. | Scanner.Slash: mustBeBinary := TRUE
  6561. | Scanner.Backslash: mustBeBinary := TRUE
  6562. | Scanner.DotSlash: mustBeBinary := TRUE
  6563. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6564. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6565. | Scanner.Not: mustBeUnary := TRUE
  6566. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6567. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6568. | Scanner.Transpose: mustBeUnary := TRUE;
  6569. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6570. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6571. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6572. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6573. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6574. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6575. | Global.Abs: mustBeUnary := TRUE;
  6576. | Global.Ash: (* TODO: arity? *)
  6577. | Global.Cap: (* TODO: arity? *)
  6578. | Global.Chr: mustBeUnary := TRUE;
  6579. | Global.Entier: (* TODO: arity? *)
  6580. | Global.EntierH: (* TODO: arity? *)
  6581. | Global.Len: (* unary and binary *)
  6582. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6583. | Global.Max, Global.Min: (* unary and binary *)
  6584. | Global.Odd: (* TODO: arity? *)
  6585. | Global.Sum: (* TODO: arity? *)
  6586. | Global.All: (* TODO: arity? *)
  6587. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6588. | Scanner.Alias:
  6589. | Scanner.GreaterGreater, Scanner.LessLess:
  6590. mustBeBinary := TRUE; hasReturnType := FALSE;
  6591. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6592. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6593. END;
  6594. (* check parameter count *)
  6595. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6596. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6597. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6598. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6599. END;
  6600. (* check parameter types *)
  6601. (* TODO: is this used at all? *)
  6602. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6603. leftType := procedureType.firstParameter.type;
  6604. rightType := procedureType.firstParameter.nextParameter.type;
  6605. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6606. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6607. END
  6608. END;
  6609. (* check return type *)
  6610. IF hasReturnType THEN
  6611. IF procedureType.returnType = NIL THEN
  6612. Error(operator.position, Diagnostics.Invalid, "return type required")
  6613. ELSIF mustReturnBoolean THEN
  6614. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6615. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6616. END
  6617. ELSIF mustReturnInteger THEN
  6618. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6619. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6620. END
  6621. END
  6622. ELSIF procedureType.returnType # NIL THEN
  6623. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6624. END
  6625. END
  6626. END
  6627. END
  6628. END VisitOperator;
  6629. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6630. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6631. BEGIN
  6632. IF error THEN RETURN FALSE END;
  6633. prevScope := currentScope;
  6634. prevDiagnostics := diagnostics;
  6635. diagnostics := NIL; (* suppress error output *)
  6636. currentScope := module.moduleScope;
  6637. VisitImport(x);
  6638. IF ~error THEN
  6639. module.moduleScope.AddImport(x);
  6640. x.SetScope(module.moduleScope);
  6641. END;
  6642. currentScope := prevScope;
  6643. diagnostics := prevDiagnostics;
  6644. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6645. END AddImport;
  6646. (** check and resolve import
  6647. - check for name = SYSTEM
  6648. - check for forbidden self import
  6649. - search through global import cache: already imported?
  6650. - check if already imported indirectly
  6651. - import if necessary -> set module and enter into import cache
  6652. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6653. - after this import this direct import and all indirect imports are stored in the current module's import list
  6654. **)
  6655. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6656. VAR
  6657. module: SyntaxTree.Module;
  6658. moduleScope: SyntaxTree.ModuleScope;
  6659. import,reimport: SyntaxTree.Import;
  6660. filename: FileName;
  6661. prevScope: SyntaxTree.Scope;
  6662. BEGIN
  6663. IF SymbolNeedsResolution(x) THEN
  6664. prevScope := currentScope;
  6665. x.SetType(SyntaxTree.importType);
  6666. moduleScope := currentScope.ownerModule.moduleScope;
  6667. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6668. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6669. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6670. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6671. ELSE
  6672. (* search through global import list: already imported ? *)
  6673. IF (x.module = NIL) & (importCache # NIL) THEN
  6674. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6675. ELSE import := NIL
  6676. END;
  6677. IF x.module # NIL THEN (* already imported indirectly *)
  6678. module := x.module;
  6679. ELSIF import # NIL THEN (* already in module list *)
  6680. module := import.module;
  6681. ASSERT(module # NIL);
  6682. x.SetModule(module);
  6683. ELSE (* must be imported *)
  6684. Global.ModuleFileName(x.moduleName,x.context,filename);
  6685. IF symbolFileFormat # NIL THEN
  6686. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6687. IF module = NIL THEN
  6688. ErrorSS(x.position,"could not import",filename);
  6689. IF VerboseErrorMessage THEN
  6690. Printout.Info("import",x)
  6691. END
  6692. ELSE
  6693. (*
  6694. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6695. (*! should rather be done by importer *)
  6696. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6697. checker.importCache := importCache;
  6698. checker.arrayBaseImported := arrayBaseImported;
  6699. checker.global := global;
  6700. checker.Module(module); (* semantic check *)
  6701. error := error OR checker.error;
  6702. END;
  6703. *)
  6704. (*
  6705. ASSERT(SyntaxTree.Resolved IN module.state);
  6706. *)
  6707. x.SetModule(module);
  6708. IF importCache # NIL THEN
  6709. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6710. import.SetContext(x.context);
  6711. import.SetModule(module);
  6712. importCache.AddImport(import);
  6713. END;
  6714. END;
  6715. ELSE
  6716. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6717. END;
  6718. END;
  6719. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6720. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6721. END;
  6722. import := module.moduleScope.firstImport;
  6723. WHILE(import # NIL) DO
  6724. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6725. ASSERT(currentScope # NIL);
  6726. ASSERT(currentScope.ownerModule # NIL);
  6727. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6728. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6729. Error(x.position,Diagnostics.Invalid,"recursive import");
  6730. ELSE
  6731. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6732. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6733. IF reimport = NIL THEN (* indirect import *)
  6734. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6735. reimport.SetContext(import.context);
  6736. reimport.SetModule(import.module);
  6737. moduleScope.AddImport(reimport);
  6738. reimport.SetScope(moduleScope);
  6739. ELSE
  6740. ASSERT(import.module # NIL);
  6741. reimport.SetModule(import.module); (* direct or indirect import *)
  6742. END;
  6743. END;
  6744. import := import.nextImport;
  6745. END;
  6746. END;
  6747. END;
  6748. currentScope := prevScope;
  6749. (* ELSE nothing to be done *)
  6750. x.SetState(SyntaxTree.Resolved);
  6751. END;
  6752. END VisitImport;
  6753. (*** statements ***)
  6754. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6755. VAR prev,resolved: SyntaxTree.Statement;
  6756. BEGIN
  6757. prev := resolvedStatement;
  6758. resolvedStatement := x;
  6759. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6760. activeCellsStatement := FALSE;
  6761. x.Accept(SELF);
  6762. (* removed this, implementation restriction should be resolved by backend
  6763. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6764. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6765. END;
  6766. *)
  6767. resolved := resolvedStatement;
  6768. resolvedStatement := prev;
  6769. RETURN resolved
  6770. END ResolveStatement;
  6771. (** check and resolve statement sequence
  6772. - check all statements, replace if necessary
  6773. **)
  6774. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6775. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6776. BEGIN
  6777. IF statementSequence # NIL THEN (* else empty *)
  6778. FOR i := 0 TO statementSequence.Length()-1 DO
  6779. statement := statementSequence.GetStatement(i);
  6780. resolved := ResolveStatement(statement);
  6781. IF (resolved # statement) THEN
  6782. statementSequence.SetStatement(i,resolved);
  6783. END;
  6784. END;
  6785. END;
  6786. END StatementSequence;
  6787. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6788. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6789. - check if procedure is callable
  6790. - check return type = NIL (otherwise must be assignment statement)
  6791. **)
  6792. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6793. VAR call: SyntaxTree.Designator;
  6794. BEGIN
  6795. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6796. call := procedureCall.call;
  6797. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6798. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6799. END;
  6800. call := ResolveDesignator(call);
  6801. IF call = SyntaxTree.invalidDesignator THEN
  6802. (* error already handled *)
  6803. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6804. (* inline call in a statement *)
  6805. ELSIF ~IsCallable(call) THEN
  6806. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6807. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6808. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6809. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6810. END;
  6811. procedureCall.SetCall(call);
  6812. (*
  6813. IF call = SyntaxTree.invalidDesignator THEN
  6814. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6815. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6816. IF IsOberonInline(procedure) THEN
  6817. Warning(procedure.position,"call to inline proc");
  6818. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6819. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6820. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6821. resolvedStatement := block;
  6822. RETURN;
  6823. END;
  6824. END;
  6825. *)
  6826. END VisitProcedureCallStatement;
  6827. (** check and resolve assignment LHS := RHS
  6828. - resolve LHS and RHS
  6829. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6830. - check if assignment is compatible
  6831. - check if LHS is variable (i.e. assignable)
  6832. - convert RHS if necessary
  6833. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6834. - assignment between different ASOTs
  6835. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6836. - assignment to ASOT elements:
  6837. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6838. **)
  6839. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6840. VAR
  6841. left: SyntaxTree.Designator;
  6842. right, expression: SyntaxTree.Expression;
  6843. designator: SyntaxTree.Designator;
  6844. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6845. mathArrayType: SyntaxTree.MathArrayType;
  6846. BEGIN
  6847. right := ResolveExpression(assignment.right);
  6848. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6849. left := ResolveDesignator(assignment.left);
  6850. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6851. (* error already handled *)
  6852. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6853. (* LHS is index write operator call on ASOT *)
  6854. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6855. (* necessary ?
  6856. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6857. type := procedureType.firstParameter.type;
  6858. expression := procedureCallDesignator.parameters.GetExpression(0);
  6859. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6860. *)
  6861. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6862. ELSIF CheckVariable(left) THEN
  6863. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6864. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6865. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6866. (* conversion done by procedure call
  6867. (* try to convert to left argument *)
  6868. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6869. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6870. procedureCallDesignator.parameters.SetExpression(1, right);
  6871. END;
  6872. *)
  6873. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6874. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6875. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6876. ELSIF AssignmentCompatible(left, right) THEN
  6877. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6878. mathArrayType := MathArrayStructureOfType(left.type);
  6879. right := NewConversion(right.position, right, mathArrayType, NIL);
  6880. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6881. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6882. ELSE
  6883. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6884. assignment.SetLeft(left);
  6885. assignment.SetRight(right);
  6886. resolvedStatement := assignment
  6887. END
  6888. END
  6889. END
  6890. END VisitAssignment;
  6891. (** check and resolve assignment LHS := RHS
  6892. - resolve LHS and RHS
  6893. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6894. - check if assignment is compatible
  6895. - check if LHS is variable (i.e. assignable)
  6896. - convert RHS if necessary
  6897. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6898. - assignment between different ASOTs
  6899. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6900. - assignment to ASOT elements:
  6901. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6902. **)
  6903. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6904. VAR
  6905. left: SyntaxTree.Designator;
  6906. right: SyntaxTree.Expression;
  6907. inPort, outPort: SyntaxTree.PortType;
  6908. expression: SyntaxTree.Expression;
  6909. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6910. BEGIN
  6911. right := ResolveExpression(communication.right);
  6912. left := ResolveDesignator(communication.left);
  6913. communication.SetLeft(left);
  6914. communication.SetRight(right);
  6915. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6916. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6917. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6918. (* conversion done by procedure call
  6919. (* try to convert to left argument *)
  6920. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6921. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6922. procedureCallDesignator.parameters.SetExpression(1, right);
  6923. END;
  6924. *)
  6925. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6926. ELSE
  6927. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6928. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6929. (* error already handled *)
  6930. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6931. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6932. IF outPort.direction # SyntaxTree.OutPort THEN
  6933. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6934. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6935. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6936. ELSE
  6937. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6938. communication.SetRight(right)
  6939. END;
  6940. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6941. IF CheckVariable(left) THEN
  6942. IF inPort.direction # SyntaxTree.InPort THEN
  6943. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6944. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6945. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6946. END;
  6947. END;
  6948. ELSE
  6949. Error(communication.position, -1, "unsupported stream operation");
  6950. END;
  6951. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6952. IF outPort.direction # SyntaxTree.OutPort THEN
  6953. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6954. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6955. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6956. ELSE
  6957. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6958. communication.SetRight(right)
  6959. END;
  6960. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6961. IF CheckVariable(right) THEN
  6962. IF inPort.direction # SyntaxTree.InPort THEN
  6963. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6964. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6965. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6966. END;
  6967. END;
  6968. ELSE
  6969. Error(communication.position, -1, "unsupported operation");
  6970. END;
  6971. END;
  6972. END VisitCommunicationStatement;
  6973. (** check and resolve if/eslif part
  6974. - check condition
  6975. - check statement sequence
  6976. **)
  6977. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6978. VAR prevUnreachable, b: BOOLEAN;
  6979. BEGIN
  6980. prevUnreachable := currentIsUnreachable;
  6981. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6982. IF IsBooleanValue(ifPart.condition,b) THEN
  6983. IF b=FALSE THEN
  6984. currentIsUnreachable := TRUE
  6985. ELSIF b=TRUE THEN
  6986. true := TRUE
  6987. END;
  6988. END;
  6989. StatementSequence(ifPart.statements);
  6990. currentIsUnreachable := prevUnreachable;
  6991. END IfPart;
  6992. (** check and resolve if statement
  6993. - check if parts and else part statement sequence
  6994. **)
  6995. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6996. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6997. BEGIN
  6998. prevUnreachable := currentIsUnreachable;
  6999. ifPartTrue := FALSE;
  7000. IfPart(ifStatement.ifPart,ifPartTrue);
  7001. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7002. elsif := ifStatement.GetElsifPart(i);
  7003. IfPart(elsif,ifPartTrue);
  7004. END;
  7005. IF ifStatement.elsePart # NIL THEN
  7006. IF ifPartTrue THEN
  7007. currentIsUnreachable := TRUE
  7008. END;
  7009. StatementSequence(ifStatement.elsePart)
  7010. END;
  7011. currentIsUnreachable := prevUnreachable;
  7012. END VisitIfStatement;
  7013. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7014. VAR variable: SyntaxTree.Designator;
  7015. type,variableType: SyntaxTree.Type;
  7016. withEntry: WithEntry;
  7017. BEGIN
  7018. variable := ResolveDesignator(withPart.variable);
  7019. variableType := variable.type.resolved;
  7020. withPart.SetVariable(variable);
  7021. type := ResolveType(withPart.type);
  7022. withPart.SetType(type);
  7023. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7024. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7025. END;
  7026. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7027. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7028. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  7029. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7030. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  7031. IF VerboseErrorMessage THEN
  7032. Printout.Info("variable",variable)
  7033. END;
  7034. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7035. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7036. IF VerboseErrorMessage THEN
  7037. Printout.Info("variable",variable);
  7038. Printout.Info("type",type);
  7039. END;
  7040. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7041. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7042. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7043. IF VerboseErrorMessage THEN
  7044. Printout.Info("variable",variable);
  7045. Printout.Info("type",type);
  7046. END;
  7047. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7048. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7049. ELSE
  7050. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7051. NEW(withEntry);
  7052. withEntry.previous := withEntries;
  7053. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7054. withEntry.type := type;
  7055. withEntries := withEntry;
  7056. StatementSequence(withPart.statements);
  7057. withEntries := withEntries.previous;
  7058. END;
  7059. END WithPart;
  7060. (** check and resolve with statement WITH variable: type DO ... END;
  7061. - check type and variable
  7062. - check that variable type is type extension of type
  7063. - check that variable is a variable
  7064. - enter new with scope and enter guardedVariable with same name and reference to variable
  7065. - create if statement:
  7066. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7067. **)
  7068. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7069. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7070. BEGIN
  7071. prevScope := currentScope; symbol := NIL;
  7072. FOR i := 0 TO withStatement.WithParts()-1 DO
  7073. WithPart(withStatement.GetWithPart(i),symbol);
  7074. END;
  7075. IF withStatement.elsePart # NIL THEN
  7076. StatementSequence(withStatement.elsePart)
  7077. END;
  7078. currentScope := prevScope;
  7079. END VisitWithStatement;
  7080. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7081. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7082. - check 'first' < 'last' and no overlaps between different case labels
  7083. - check statement sequence
  7084. **)
  7085. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7086. VAR
  7087. i: LONGINT;
  7088. position: LONGINT;
  7089. expression, left, right: SyntaxTree.Expression;
  7090. expressionType: SyntaxTree.Type;
  7091. l, r: LONGINT;
  7092. cl, cr: CHAR;
  7093. thiscases: SyntaxTree.CaseConstant;
  7094. BEGIN
  7095. thiscases := NIL;
  7096. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7097. expression := casePart.elements.GetExpression(i);
  7098. position := expression.position;
  7099. (* set context of range *)
  7100. IF expression IS SyntaxTree.RangeExpression THEN
  7101. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7102. END;
  7103. expression := ResolveExpression(expression);
  7104. IF expression = SyntaxTree.invalidExpression THEN
  7105. (* error already reported *)
  7106. expressionType := SyntaxTree.invalidType;
  7107. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7108. (* read out 'first' and 'last' *)
  7109. left := expression(SyntaxTree.RangeExpression).first;
  7110. right := expression(SyntaxTree.RangeExpression).last;
  7111. (* guaranteed by VisitRangeExpression: *)
  7112. ASSERT((left # NIL) & (right # NIL));
  7113. ASSERT(left.type.resolved = right.type.resolved);
  7114. left := CompatibleConversion(left.position, left, type);
  7115. right := CompatibleConversion(right.position, right, type);
  7116. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7117. expression(SyntaxTree.RangeExpression).SetLast(right);
  7118. expressionType := RegularType(position,left.type);
  7119. ELSE
  7120. expression := ConstantExpression(expression);
  7121. expression := CompatibleConversion(expression.position, expression, type);
  7122. (*
  7123. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7124. left := Global.NewCharacterValue(system,expression.position,cl);
  7125. expression := casePart.elements.GetExpression(i);
  7126. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7127. expression := left
  7128. END;
  7129. *)
  7130. casePart.elements.SetExpression(i,expression);
  7131. left := expression; right := expression;
  7132. expressionType := RegularType(position,expression.type)
  7133. END;
  7134. IF (expressionType = SyntaxTree.invalidType) THEN
  7135. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7136. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7137. expression := SyntaxTree.invalidExpression;
  7138. ELSE
  7139. l := 0; r := 0;
  7140. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7141. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7142. l := ORD(cl); r := ORD(cr);
  7143. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7144. ELSE
  7145. expression := SyntaxTree.invalidExpression
  7146. END;
  7147. IF expression # SyntaxTree.invalidExpression THEN
  7148. IF l>r THEN
  7149. Error(position,Diagnostics.Invalid,"empty case label")
  7150. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7151. Error(position,Diagnostics.Invalid,"duplicate case label");
  7152. ELSE
  7153. IF l < min THEN min := l END;
  7154. IF r > max THEN max := r END;
  7155. END;
  7156. END;
  7157. END;
  7158. casePart.elements.SetExpression(i,expression);
  7159. END;
  7160. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7161. casePart.SetConstants(thiscases);
  7162. StatementSequence(casePart.statements);
  7163. END CasePart;
  7164. (** check and resolve case statement CASE variable OF ... END;
  7165. - check variable
  7166. - check case parts
  7167. **)
  7168. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7169. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7170. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7171. BEGIN
  7172. expression := ResolveExpression(caseStatement.variable);
  7173. type := RegularType(expression.position,expression.type);
  7174. IF type = SyntaxTree.invalidType THEN
  7175. expression := SyntaxTree.invalidExpression;
  7176. ELSIF IsIntegerType(type) THEN
  7177. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7178. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7179. (*
  7180. expression := Global.NewCharacterValue(system,expression.position,ch);
  7181. *)
  7182. type := expression.type;
  7183. ELSIF IsCharacterType(type) THEN
  7184. ELSIF IsEnumerationType(type) THEN
  7185. ELSE
  7186. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7187. expression := SyntaxTree.invalidExpression;
  7188. END;
  7189. caseStatement.SetVariable(expression);
  7190. caseList := NIL;
  7191. min := MAX(LONGINT); max := MIN(LONGINT);
  7192. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7193. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7194. END;
  7195. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7196. msg := "huge sparse case table ";
  7197. Strings.AppendInt(msg, max-min);
  7198. Strings.Append(msg,"/");
  7199. Strings.AppendInt(msg, caseStatement.CaseParts());
  7200. Warning(caseStatement.position,msg);
  7201. END;
  7202. caseStatement.SetMinMax(min,max);
  7203. StatementSequence(caseStatement.elsePart);
  7204. IF expression.resolved # NIL THEN
  7205. IF IsCharacterValue(expression,ch) THEN
  7206. l := ORD(ch)
  7207. ELSIF IsIntegerValue(expression,l) THEN
  7208. END;
  7209. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7210. END;
  7211. END VisitCaseStatement;
  7212. (** check and resolve while statement
  7213. - check condition
  7214. - check statement sequence
  7215. **)
  7216. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7217. VAR prevIsUnreachable,b: BOOLEAN;
  7218. BEGIN
  7219. prevIsUnreachable := currentIsUnreachable;
  7220. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7221. IF IsBooleanValue(whileStatement.condition,b) THEN
  7222. IF b=FALSE THEN
  7223. currentIsUnreachable := TRUE
  7224. END;
  7225. END;
  7226. StatementSequence(whileStatement.statements);
  7227. currentIsUnreachable := prevIsUnreachable
  7228. END VisitWhileStatement;
  7229. (** check and resolve repeat statement
  7230. - check condition
  7231. - check statement sequence
  7232. **)
  7233. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7234. BEGIN
  7235. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7236. StatementSequence(repeatStatement.statements);
  7237. END VisitRepeatStatement;
  7238. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7239. VAR withEntry: WithEntry;
  7240. BEGIN
  7241. withEntry := withEntries;
  7242. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7243. withEntry := withEntry.previous
  7244. END;
  7245. IF withEntry = NIL THEN RETURN FALSE
  7246. ELSE
  7247. type := withEntry.type;
  7248. RETURN TRUE
  7249. END;
  7250. END GetGuard;
  7251. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7252. - check that variable is an integer variable
  7253. - check that from is integer typed with compatible type
  7254. - check that to has compatible type
  7255. - check that by is constant integer with compatible type
  7256. **)
  7257. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7258. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7259. BEGIN
  7260. designator := ResolveDesignator(forStatement.variable);
  7261. type := SyntaxTree.invalidType;
  7262. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7263. designator := SyntaxTree.invalidDesignator;
  7264. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7265. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7266. designator := SyntaxTree.invalidDesignator;
  7267. ELSIF CheckVariable(designator) THEN
  7268. type := designator.type;
  7269. END;
  7270. forStatement.SetVariable(designator);
  7271. expression := ResolveExpression(forStatement.from);
  7272. IF expression = SyntaxTree.invalidExpression THEN
  7273. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7274. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7275. expression := SyntaxTree.invalidExpression;
  7276. ELSIF type # SyntaxTree.invalidType THEN
  7277. expression := NewConversion(expression.position,expression,type,NIL)
  7278. END;
  7279. forStatement.SetFrom(expression);
  7280. expression := ResolveExpression(forStatement.to);
  7281. IF expression = SyntaxTree.invalidExpression THEN
  7282. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7283. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7284. expression := SyntaxTree.invalidExpression;
  7285. ELSIF type # SyntaxTree.invalidType THEN
  7286. expression := NewConversion(expression.position,expression,type,NIL)
  7287. END;
  7288. forStatement.SetTo(expression);
  7289. IF forStatement.by # NIL THEN
  7290. expression := ConstantInteger(forStatement.by);
  7291. ELSE
  7292. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7293. END;
  7294. IF expression = SyntaxTree.invalidExpression THEN
  7295. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7296. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7297. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7298. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7299. ELSIF type # SyntaxTree.invalidType THEN
  7300. expression := NewConversion(expression.position,expression,type,NIL)
  7301. END;
  7302. forStatement.SetBy(expression);
  7303. StatementSequence(forStatement.statements);
  7304. END VisitForStatement;
  7305. (** check and resolve loop statement LOOP StatementSequence END
  7306. - check statement sequence
  7307. **)
  7308. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7309. BEGIN
  7310. StatementSequence(loopStatement.statements)
  7311. END VisitLoopStatement;
  7312. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7313. BEGIN
  7314. StatementSequence(exitableBlock.statements);
  7315. END VisitExitableBlock;
  7316. (** check and resolve exit statement EXIT
  7317. - check that exit is within LOOP statement block
  7318. **)
  7319. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7320. VAR outer: SyntaxTree.Statement;
  7321. BEGIN
  7322. outer := exitStatement.outer;
  7323. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7324. outer := outer.outer;
  7325. END;
  7326. IF outer = NIL THEN
  7327. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7328. END;
  7329. END VisitExitStatement;
  7330. (** check and resolve return statement RETURN [expression]
  7331. - check expression (if any)
  7332. - check if in procedure scope
  7333. - if in procedure scope then check expression compatibility
  7334. - if not in procecdure scope then check on return without expression
  7335. **)
  7336. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7337. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7338. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7339. BEGIN
  7340. position := returnStatement.position;
  7341. expression := returnStatement.returnValue;
  7342. IF expression # NIL THEN
  7343. expression := ResolveExpression(expression);
  7344. returnStatement.SetReturnValue(expression);
  7345. END;
  7346. outer := returnStatement.outer;
  7347. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7348. outer := outer.outer
  7349. END;
  7350. IF (outer # NIL) THEN
  7351. scope := outer(SyntaxTree.Body).inScope;
  7352. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7353. IF (expression # NIL) THEN
  7354. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7355. END;
  7356. ELSE
  7357. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7358. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7359. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7360. END;
  7361. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7362. IF returnType # NIL THEN
  7363. returnType := returnType.resolved;
  7364. IF expression = NIL THEN
  7365. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7366. ELSIF expression.type = NIL THEN
  7367. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7368. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7369. Error(position,Diagnostics.Invalid,"return type not compatible");
  7370. IF VerboseErrorMessage THEN
  7371. Printout.Info("returnType",returnType);
  7372. Printout.Info("expression",expression);
  7373. END;
  7374. ELSE
  7375. expression := NewConversion(expression.position,expression,returnType,NIL);
  7376. returnStatement.SetReturnValue(expression);
  7377. END;
  7378. ELSIF expression # NIL THEN
  7379. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7380. END;
  7381. END;
  7382. END;
  7383. END VisitReturnStatement;
  7384. (** check and resolve await statement AWAIT(condition: Expression)
  7385. - check await condition
  7386. **)
  7387. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7388. VAR condition: SyntaxTree.Expression;
  7389. BEGIN
  7390. condition := ResolveCondition(awaitStatement.condition);
  7391. IF currentIsRealtime THEN
  7392. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7393. END;
  7394. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7395. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7396. END;
  7397. awaitStatement.SetCondition(condition);
  7398. END VisitAwaitStatement;
  7399. PROCEDURE CheckSystemImport(position: LONGINT);
  7400. VAR import: SyntaxTree.Import;
  7401. BEGIN
  7402. import := currentScope.ownerModule.moduleScope.firstImport;
  7403. WHILE(import # NIL) DO
  7404. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7405. RETURN;
  7406. END;
  7407. import := import.nextImport;
  7408. END;
  7409. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7410. END CheckSystemImport;
  7411. (** check and resolve code statement: do nothing, must be done by assembler
  7412. **)
  7413. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7414. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7415. BEGIN
  7416. CheckSystemImport(code.position);
  7417. FOR i := 0 TO code.inRules.Length()-1 DO
  7418. statement := code.inRules.GetStatement(i);
  7419. IF statement IS SyntaxTree.Assignment THEN
  7420. WITH statement: SyntaxTree.Assignment DO
  7421. statement.SetRight(ResolveExpression(statement.right));
  7422. END;
  7423. ELSE
  7424. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7425. END;
  7426. END;
  7427. FOR i := 0 TO code.outRules.Length()-1 DO
  7428. statement := code.outRules.GetStatement(i);
  7429. IF statement IS SyntaxTree.Assignment THEN
  7430. WITH statement: SyntaxTree.Assignment DO
  7431. statement.SetLeft(ResolveDesignator(statement.left));
  7432. END;
  7433. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7434. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7435. ELSE
  7436. Printout.Info("out statement ", statement);
  7437. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7438. END;
  7439. END;
  7440. END VisitCode;
  7441. (** check and set flags of a statement block
  7442. - check for multiply occurence of a flag
  7443. - check and set priority only in bodies
  7444. - check for valid names
  7445. **)
  7446. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7447. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7448. flag: LONGINT; recordBody: SyntaxTree.Body;
  7449. PROCEDURE SetProtectedRecord;
  7450. VAR scope: SyntaxTree.Scope;
  7451. BEGIN
  7452. scope := currentScope;
  7453. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7454. scope := scope.outerScope
  7455. END;
  7456. IF scope # NIL THEN
  7457. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7458. END;
  7459. END SetProtectedRecord;
  7460. BEGIN
  7461. flags := {};
  7462. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7463. recordBody := block(SyntaxTree.Body)
  7464. ELSE
  7465. recordBody := NIL
  7466. END;
  7467. blockModifier := block.blockModifiers;
  7468. WHILE(blockModifier # NIL) DO
  7469. name := blockModifier.identifier;
  7470. expression := blockModifier.expression;
  7471. position := blockModifier.position;
  7472. flag := -1;
  7473. IF name=Global.NamePriority THEN
  7474. IF expression = NIL THEN
  7475. Error(position,Diagnostics.Invalid,"missing priority expression");
  7476. ELSIF recordBody = NIL THEN
  7477. Error(position,Diagnostics.Invalid,"priority not on record body");
  7478. ELSIF recordBody.priority # NIL THEN
  7479. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7480. ELSE
  7481. recordBody.SetPriority(expression);
  7482. END;
  7483. ELSIF expression # NIL THEN
  7484. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7485. ELSIF name=Global.NameExclusive THEN
  7486. IF block.isExclusive THEN
  7487. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7488. END;
  7489. block.SetExclusive(TRUE); SetProtectedRecord;
  7490. ELSIF name=Global.NameActive THEN
  7491. IF recordBody = NIL THEN
  7492. Error(position,Diagnostics.Invalid,"active not in record body");
  7493. ELSIF recordBody.isActive THEN
  7494. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7495. ELSE
  7496. recordBody.SetActive(TRUE); SetProtectedRecord;
  7497. END;
  7498. ELSIF name=Global.NameSafe THEN
  7499. IF recordBody = NIL THEN
  7500. Error(position,Diagnostics.Invalid,"safe not in record body");
  7501. ELSIF recordBody.isSafe THEN
  7502. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7503. ELSE
  7504. recordBody.SetSafe(TRUE);
  7505. SetProtectedRecord;
  7506. END;
  7507. ELSIF name=Global.NameRealtime THEN
  7508. IF recordBody = NIL THEN
  7509. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7510. ELSIF recordBody.isRealtime THEN
  7511. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7512. ELSE
  7513. recordBody.SetRealtime(TRUE);
  7514. block.SetRealtime(TRUE);
  7515. END;
  7516. ELSIF name=Global.NameUnchecked THEN
  7517. IF block.isUnchecked THEN
  7518. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7519. ELSE
  7520. block.SetUnchecked(TRUE);
  7521. END;
  7522. ELSIF (name=Global.NameUncooperative) THEN
  7523. IF block.isUncooperative THEN
  7524. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7525. ELSE
  7526. block.SetUncooperative(TRUE);
  7527. END;
  7528. ELSE
  7529. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7530. END;
  7531. blockModifier := blockModifier.nextModifier;
  7532. END;
  7533. END BlockFlags;
  7534. (** check and resolve statement block
  7535. - check flags (exclusive)
  7536. - check statement sequence
  7537. **)
  7538. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7539. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7540. BEGIN
  7541. BlockFlags(statementBlock);
  7542. IF statementBlock.isExclusive THEN
  7543. (* check that not in exclusive block *)
  7544. IF currentIsExclusive THEN
  7545. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7546. ELSIF currentIsRealtime THEN
  7547. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7548. END;
  7549. END;
  7550. recentExclusive := currentIsExclusive;
  7551. recentUnreachable := currentIsUnreachable;
  7552. recentRealtime := currentIsRealtime;
  7553. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7554. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7555. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7556. StatementSequence(statementBlock.statements);
  7557. currentIsRealtime := recentRealtime;
  7558. currentIsExclusive := recentExclusive;
  7559. currentIsUnreachable := recentUnreachable;
  7560. END VisitStatementBlock;
  7561. (** check and resolve body
  7562. - check flags (active, priority, safe)
  7563. - check body and finally part
  7564. **)
  7565. PROCEDURE Body(body: SyntaxTree.Body);
  7566. BEGIN
  7567. VisitStatementBlock(body);
  7568. IF body.isActive THEN
  7569. IF ~currentIsBodyProcedure THEN
  7570. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7571. ELSIF body.priority # NIL THEN
  7572. body.SetPriority(ConstantInteger(body.priority));
  7573. END;
  7574. ELSIF body.isSafe THEN
  7575. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7576. ELSIF body.priority # NIL THEN
  7577. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7578. END;
  7579. IF body.code # NIL THEN
  7580. CheckSystemImport(body.position);
  7581. END;
  7582. StatementSequence(body.finally)
  7583. END Body;
  7584. (*** scopes ***)
  7585. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7586. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7587. VAR duplicateSymbol: BOOLEAN;
  7588. BEGIN
  7589. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7590. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7591. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7592. END;
  7593. scope.EnterSymbol(symbol,duplicateSymbol);
  7594. IF ~allowDuplicate & duplicateSymbol THEN
  7595. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7596. IF VerboseErrorMessage THEN
  7597. Printout.Info("multiply defined identifier",symbol);
  7598. Printout.Info("in scope",scope);
  7599. END;
  7600. END;
  7601. END Register;
  7602. (**
  7603. implementation: check and resolve an implementation part
  7604. **)
  7605. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7606. move implementation checker to a separate object ? *)
  7607. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7608. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7609. BEGIN
  7610. prevIsRealtime := currentIsRealtime;
  7611. prevIsBodyProcedure := currentIsBodyProcedure;
  7612. prevIsCellNet := currentIsCellNet;
  7613. prevScope := currentScope;
  7614. currentScope := scope;
  7615. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7616. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7617. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7618. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7619. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7620. (*
  7621. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7622. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7623. END;
  7624. *)
  7625. END;
  7626. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7627. (* module body, record bodies are wrapped into an artifical procedure *)
  7628. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7629. Body(scope(SyntaxTree.ProcedureScope).body)
  7630. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7631. Body(scope(SyntaxTree.ProcedureScope).body)
  7632. END;
  7633. END;
  7634. currentScope := prevScope;
  7635. currentIsRealtime := prevIsRealtime;
  7636. currentIsBodyProcedure := prevIsBodyProcedure;
  7637. currentIsCellNet := prevIsCellNet;
  7638. END Implementation;
  7639. (** implementation phase:
  7640. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7641. **)
  7642. PROCEDURE Implementations(x: SyntaxTree.Module);
  7643. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7644. BEGIN
  7645. prevPhase := phase;
  7646. phase := InlinePhase;
  7647. scope := x.firstScope;
  7648. WHILE(scope # NIL) DO
  7649. Implementation(scope);
  7650. scope := scope.nextScope;
  7651. END;
  7652. phase := ImplementationPhase;
  7653. scope := x.firstScope;
  7654. WHILE(scope # NIL) DO
  7655. Implementation(scope);
  7656. scope := scope.nextScope;
  7657. END;
  7658. phase := prevPhase;
  7659. END Implementations;
  7660. (** declaration phase:
  7661. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7662. - import lists (for module scopes)
  7663. - parameter list (for procedure scopes)
  7664. - constant declarations
  7665. - type declarations
  7666. - variable declarations
  7667. - procedure declarations
  7668. preformed in two stages:
  7669. - first all symbols are entered into the symbol table (with uniqueness check),
  7670. - then all symbols are resolved
  7671. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7672. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7673. **)
  7674. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7675. VAR
  7676. constant: SyntaxTree.Constant;
  7677. typeDeclaration: SyntaxTree.TypeDeclaration;
  7678. variable: SyntaxTree.Variable;
  7679. procedure: SyntaxTree.Procedure;
  7680. prevScope: SyntaxTree.Scope;
  7681. parameter: SyntaxTree.Parameter;
  7682. import: SyntaxTree.Import;
  7683. symbol: SyntaxTree.Symbol;
  7684. prevPhase: LONGINT;
  7685. prevError : BOOLEAN;
  7686. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7687. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7688. BEGIN
  7689. IF type.baseType # NIL THEN
  7690. baseType := type.baseType.resolved;
  7691. IF baseType IS SyntaxTree.PointerType THEN
  7692. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7693. END;
  7694. (*
  7695. IF baseType IS SyntaxTree.CellType THEN
  7696. DeclareCell(baseType(SyntaxTree.CellType));
  7697. END;
  7698. *)
  7699. END;
  7700. parameter := type.firstParameter;
  7701. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7702. (*
  7703. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7704. variable.SetType(parameter.type);
  7705. variable.SetAccess(SyntaxTree.Hidden);
  7706. variable.SetModifiers(parameter.modifiers);
  7707. currentScope.PushVariable(variable);
  7708. *)
  7709. Register(parameter,scope, FALSE);
  7710. parameter := parameter.nextParameter;
  7711. END;
  7712. property := type.firstProperty;
  7713. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7714. (*
  7715. variable := currentScope.FindVariable(property.name);
  7716. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7717. prop := variable(SyntaxTree.Property);
  7718. ELSE (* add, duplicate symbols detection later *)
  7719. prop := SyntaxTree.NewProperty(property.position, property.name);
  7720. currentScope.PushVariable(prop);
  7721. END;
  7722. prop.SetType(property.type);
  7723. prop.SetValue(property.value);
  7724. prop.SetAccess(SyntaxTree.Hidden);
  7725. *)
  7726. Register(property, scope, FALSE);
  7727. property := property.nextProperty;
  7728. END;
  7729. END DeclareCell;
  7730. BEGIN
  7731. prevError := error;
  7732. prevPhase := phase;
  7733. phase := DeclarationPhase;
  7734. prevScope := currentScope;
  7735. currentScope := scope;
  7736. error := FALSE;
  7737. (* first enter all symbols in scope *)
  7738. IF scope IS SyntaxTree.ModuleScope THEN
  7739. (* treat imports first for a module scope, , set default context if necessary *)
  7740. import := scope(SyntaxTree.ModuleScope).firstImport;
  7741. WHILE(import # NIL) DO
  7742. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7743. Register(import, currentScope, FALSE);
  7744. import := import.nextImport;
  7745. END;
  7746. import := scope(SyntaxTree.ModuleScope).firstImport;
  7747. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7748. ResolveSymbol(import);
  7749. import := import.nextImport;
  7750. END;
  7751. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7752. (* enter parameters for a procedure scope *)
  7753. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7754. WHILE(parameter # NIL) DO
  7755. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7756. END;
  7757. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7758. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7759. ELSIF scope IS SyntaxTree.CellScope THEN
  7760. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7761. IF~skipImplementation THEN
  7762. import := scope(SyntaxTree.CellScope).firstImport;
  7763. WHILE(import # NIL) DO
  7764. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7765. Register(import, currentScope, FALSE);
  7766. import := import.nextImport;
  7767. END;
  7768. import := scope(SyntaxTree.CellScope).firstImport;
  7769. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7770. ResolveSymbol(import);
  7771. import := import.nextImport;
  7772. END;
  7773. END;
  7774. END;
  7775. IF error THEN RETURN END;
  7776. IF skipImplementation THEN
  7777. scope.Clear;
  7778. END;
  7779. (* constants *)
  7780. constant := scope.firstConstant;
  7781. WHILE (constant # NIL) DO
  7782. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7783. END;
  7784. (* type declarations *)
  7785. typeDeclaration := scope.firstTypeDeclaration;
  7786. WHILE (typeDeclaration # NIL) DO
  7787. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7788. END;
  7789. (* variables *)
  7790. variable := scope.firstVariable;
  7791. WHILE (variable # NIL) DO
  7792. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7793. END;
  7794. (* procedures *)
  7795. procedure := scope.firstProcedure;
  7796. WHILE (procedure # NIL) DO
  7797. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7798. END;
  7799. (* now process all symbols without any presumption on the order *)
  7800. symbol := scope.firstSymbol;
  7801. WHILE(symbol # NIL) DO
  7802. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7803. ResolveSymbol(symbol);
  7804. END;
  7805. symbol := symbol.nextSymbol;
  7806. END;
  7807. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7808. symbol := scope.firstSymbol;
  7809. WHILE symbol # NIL DO
  7810. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7811. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7812. pointerFixes.Add(symbol, currentScope);
  7813. END;
  7814. IF ~symbol.type.resolved.isRealtime THEN
  7815. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7816. END;
  7817. END;
  7818. symbol := symbol.nextSymbol
  7819. END;
  7820. END;
  7821. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7822. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7823. END;
  7824. IF (scope.ownerModule # NIL) THEN
  7825. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7826. scope.ownerModule.AddScope(scope);
  7827. END;
  7828. phase := prevPhase;
  7829. currentScope := prevScope;
  7830. error := error OR prevError;
  7831. END Declarations;
  7832. (* nopov *)
  7833. (** check if all operators from one module are compatible to the ones in the other module
  7834. - check if there are not multiple operators with the same signature
  7835. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7836. - check for all operators whose signatures are compatible, whether the return types are compatible
  7837. note that:
  7838. - the return type is not considered to be part of the signature
  7839. - two signatures are considered compatible, if all of the operands are compatible
  7840. **)
  7841. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7842. VAR
  7843. thisOperator, thatOperator: SyntaxTree.Operator;
  7844. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7845. thisParameter, thatParameter: SyntaxTree.Parameter;
  7846. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7847. i: LONGINT;
  7848. BEGIN
  7849. currentScope := thisModuleScope;
  7850. hasError := FALSE;
  7851. (* go through all operators in the other module *)
  7852. thatOperator := thatModuleScope.firstOperator;
  7853. WHILE (thatOperator # NIL) & ~hasError DO
  7854. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7855. (* the other operator is accessible *)
  7856. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7857. (* the other operator is not the conversion operator *)
  7858. (* go through all operators in this module *)
  7859. thisOperator := thisModuleScope.firstOperator;
  7860. WHILE (thisOperator # NIL) & ~hasError DO
  7861. IF thisOperator # thatOperator THEN
  7862. (* the operators are not the same *)
  7863. IF thisOperator.name = thatOperator.name THEN
  7864. (* the operators share the same identifier *)
  7865. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7866. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7867. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7868. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7869. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7870. (* both operators have the same paramter count *)
  7871. thisParameter := thisProcedureType.firstParameter;
  7872. thatParameter := thatProcedureType.firstParameter;
  7873. operandsAreEqual := TRUE;
  7874. operandsAreCompatible := TRUE;
  7875. (* go through all parameters *)
  7876. FOR i := 1 TO thisProcedureType.numberParameters DO
  7877. ASSERT(thatParameter # NIL);
  7878. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7879. operandsAreEqual := FALSE;
  7880. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7881. operandsAreCompatible := FALSE
  7882. END
  7883. END;
  7884. thisParameter := thisParameter.nextParameter;
  7885. thatParameter := thatParameter.nextParameter
  7886. END;
  7887. IF operandsAreEqual THEN
  7888. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7889. hasError := TRUE
  7890. ELSIF operandsAreCompatible THEN
  7891. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7892. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7893. hasError := TRUE
  7894. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7895. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7896. hasError := TRUE
  7897. END
  7898. END
  7899. END
  7900. END
  7901. END;
  7902. thisOperator := thisOperator.nextOperator
  7903. END
  7904. END
  7905. END;
  7906. thatOperator := thatOperator.nextOperator
  7907. END
  7908. END CheckInterOperatorConformity;
  7909. (** check module:
  7910. - check module declaration
  7911. - add context, if necessary
  7912. - remove module from import cache, if necessary
  7913. - check declarations
  7914. - resolve all type fixes
  7915. - check implementation (bodies)
  7916. **)
  7917. PROCEDURE Module*(x: SyntaxTree.Module);
  7918. VAR (* nopov *)
  7919. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7920. BEGIN
  7921. prevScope := currentScope;
  7922. prevIsCellNet := currentIsCellNet;
  7923. module := x;
  7924. ASSERT(x # NIL);
  7925. global := system.globalScope[x.case];
  7926. x.moduleScope.SetGlobalScope(global);
  7927. currentScope := global;
  7928. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7929. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7930. RemoveModuleFromCache(importCache,x);
  7931. Declarations(x.moduleScope, FALSE);
  7932. FixTypes();
  7933. IF module.isCellNet THEN
  7934. currentIsCellNet := TRUE;
  7935. modifier := x.modifiers;
  7936. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7937. CheckModifiers(modifier, FALSE);
  7938. END;
  7939. (* nopov *)
  7940. IF ~error THEN
  7941. (* check if operators conform to each other within this module *)
  7942. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7943. (* go through all imports *)
  7944. import := x.moduleScope.firstImport;
  7945. WHILE import # NIL DO
  7946. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7947. (* check if all operators in this module conform to the ones of the imported module *)
  7948. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7949. END;
  7950. import := import.nextImport
  7951. END;
  7952. END;
  7953. Implementations(x);
  7954. module := NIL;
  7955. currentIsCellNet := prevIsCellNet;
  7956. currentScope := prevScope;
  7957. END Module;
  7958. END Checker;
  7959. Warnings*=OBJECT (SyntaxTree.Visitor)
  7960. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7961. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7962. BEGIN
  7963. SELF.diagnostics := diagnostics
  7964. END InitWarnings;
  7965. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  7966. BEGIN END VisitPortType;
  7967. (** types *)
  7968. PROCEDURE Type(x: SyntaxTree.Type);
  7969. BEGIN x.Accept(SELF)
  7970. END Type;
  7971. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7972. BEGIN END VisitType;
  7973. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7974. BEGIN END VisitBasicType;
  7975. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7976. BEGIN END VisitCharacterType;
  7977. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7978. BEGIN END VisitIntegerType;
  7979. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7980. BEGIN END VisitFloatType;
  7981. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7982. BEGIN END VisitQualifiedType;
  7983. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7984. BEGIN END VisitStringType;
  7985. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7986. BEGIN END VisitEnumerationType;
  7987. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7988. BEGIN END VisitRangeType;
  7989. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7990. BEGIN
  7991. IF ~(SyntaxTree.Warned IN x.state) THEN
  7992. x.SetState(SyntaxTree.Warned);
  7993. Type(x.arrayBase);
  7994. END;
  7995. END VisitArrayType;
  7996. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7997. BEGIN
  7998. IF ~(SyntaxTree.Warned IN x.state) THEN
  7999. x.SetState(SyntaxTree.Warned);
  8000. Type(x.arrayBase);
  8001. END;
  8002. END VisitMathArrayType;
  8003. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8004. BEGIN
  8005. IF ~(SyntaxTree.Warned IN x.state) THEN
  8006. x.SetState(SyntaxTree.Warned);
  8007. Type(x.pointerBase);
  8008. END;
  8009. END VisitPointerType;
  8010. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8011. BEGIN Scope(x.recordScope) END VisitRecordType;
  8012. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8013. BEGIN Scope(x.cellScope) END VisitCellType;
  8014. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8015. BEGIN END VisitProcedureType;
  8016. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8017. VAR msg: ARRAY 256 OF CHAR;
  8018. BEGIN
  8019. Global.GetSymbolName(x,msg);
  8020. Strings.Append(msg," ");
  8021. Strings.Append(msg,text);
  8022. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  8023. END Warning;
  8024. (** symbols *)
  8025. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8026. BEGIN
  8027. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8028. IF ~(x IS SyntaxTree.Parameter) THEN
  8029. Warning(x,"never used");
  8030. END;
  8031. END;
  8032. x.Accept(SELF);
  8033. END Symbol;
  8034. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8035. BEGIN END VisitSymbol;
  8036. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8037. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8038. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8039. BEGIN END VisitConstant;
  8040. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8041. BEGIN END VisitVariable;
  8042. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8043. BEGIN END VisitProperty;
  8044. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8045. BEGIN END VisitParameter;
  8046. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8047. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8048. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8049. BEGIN END VisitOperator;
  8050. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8051. BEGIN END VisitImport;
  8052. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8053. VAR
  8054. symbol: SyntaxTree.Symbol;
  8055. BEGIN
  8056. symbol := scope.firstSymbol;
  8057. WHILE(symbol # NIL) DO
  8058. Symbol(symbol);
  8059. symbol := symbol.nextSymbol;
  8060. END;
  8061. END Scope;
  8062. PROCEDURE Module*(x: SyntaxTree.Module);
  8063. BEGIN
  8064. SELF.module := x;
  8065. Scope(x.moduleScope);
  8066. END Module;
  8067. END Warnings;
  8068. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8069. BEGIN
  8070. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8071. END IsOberonInline;
  8072. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8073. BEGIN
  8074. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8075. END Resolved;
  8076. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8077. VAR i: LONGINT;
  8078. BEGIN
  8079. i := 1;
  8080. WHILE i < x DO
  8081. i := i *2
  8082. END;
  8083. RETURN i=x
  8084. END PowerOf2;
  8085. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8086. BEGIN
  8087. RETURN
  8088. (scope # NIL) &
  8089. (scope IS SyntaxTree.ModuleScope)
  8090. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8091. OR
  8092. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8093. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8094. END IsCellNetScope;
  8095. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8096. BEGIN
  8097. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8098. END IsCellScope;
  8099. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8100. BEGIN
  8101. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8102. RETURN (scope # NIL) & IsCellNetScope(scope)
  8103. END InCellNetScope;
  8104. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8105. BEGIN
  8106. ASSERT(size MOD system.dataUnit = 0);
  8107. RETURN size DIV system.dataUnit
  8108. END ToMemoryUnits;
  8109. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8110. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8111. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8112. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8113. BEGIN
  8114. IF t = NIL THEN
  8115. RETURN TRUE
  8116. ELSE
  8117. t := t.resolved;
  8118. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8119. END;
  8120. END TypeAllowed;
  8121. BEGIN
  8122. type := type.resolved;
  8123. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8124. RETURN FALSE
  8125. ELSE
  8126. procedureType := type(SyntaxTree.ProcedureType);
  8127. numberParameters := procedureType.numberParameters;
  8128. RETURN
  8129. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8130. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8131. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8132. END;
  8133. END GetProcedureAllowed;
  8134. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8135. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8136. VAR import: SyntaxTree.Import;
  8137. BEGIN
  8138. import := importCache.ImportByModuleName(x.name,x.context);
  8139. IF import # NIL THEN
  8140. importCache.RemoveImporters(x.name,x.context);
  8141. END;
  8142. END RemoveModuleFromCache;
  8143. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8144. (* to <- this assignment compatibility *)
  8145. VAR result: BOOLEAN;
  8146. BEGIN
  8147. IF this= NIL THEN result := (to=NIL)
  8148. ELSIF to=NIL THEN result := FALSE
  8149. ELSE
  8150. (*! will be replaced by this:
  8151. ELSE result := this.CompatibleTo(to.resolved);
  8152. *)
  8153. this := this.resolved; to := to.resolved;
  8154. IF to=SyntaxTree.invalidType THEN result := FALSE
  8155. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8156. ELSIF to = this THEN
  8157. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8158. ELSIF to IS SyntaxTree.BasicType THEN
  8159. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8160. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8161. result := this.CompatibleTo(to.resolved)
  8162. ELSE
  8163. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8164. END
  8165. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8166. result := to.sizeInBits = this.sizeInBits;
  8167. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8168. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8169. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8170. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8171. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8172. result := TRUE;
  8173. ELSIF to IS SyntaxTree.AnyType THEN
  8174. result := (this IS SyntaxTree.RecordType) & this(SyntaxTree.RecordType).isObject OR (this IS SyntaxTree.PointerType) OR (this IS SyntaxTree.ProcedureType) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.AnyType) OR (this IS SyntaxTree.ObjectType);
  8175. ELSIF to IS SyntaxTree.ObjectType THEN
  8176. result := IsPointerToRecord(this) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ObjectType) OR (this IS SyntaxTree.AnyType) (*! remove when symbol file can distinguish OBJECT from ANY *) ;
  8177. ELSIF to IS SyntaxTree.ByteType THEN
  8178. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8179. ELSIF to IS SyntaxTree.CharacterType THEN
  8180. result := IsCharacterType(this)
  8181. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8182. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8183. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8184. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8185. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8186. result := TRUE;
  8187. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8188. result := TRUE;
  8189. ELSE
  8190. result := FALSE
  8191. END;
  8192. ELSIF to IS SyntaxTree.PointerType THEN
  8193. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8194. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8195. & (~to.isRealtime OR this.isRealtime);
  8196. ELSIF to IS SyntaxTree.ProcedureType THEN
  8197. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8198. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8199. & (~to.isRealtime OR this.isRealtime)
  8200. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8201. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8202. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8203. ELSIF to IS SyntaxTree.RecordType THEN
  8204. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8205. ELSIF to IS SyntaxTree.ArrayType THEN
  8206. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8207. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8208. ELSIF StaticArrayCompatible(to, this) THEN
  8209. result := TRUE
  8210. ELSE
  8211. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8212. END;
  8213. ELSIF to IS SyntaxTree.MathArrayType THEN
  8214. IF this IS SyntaxTree.MathArrayType THEN
  8215. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8216. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8217. result := TRUE;
  8218. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8219. result := TRUE;
  8220. ELSE
  8221. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8222. END;
  8223. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8224. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8225. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8226. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8227. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8228. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8229. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8230. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8231. ELSE
  8232. result := FALSE
  8233. END;
  8234. (* an array-structured object type is compatible to the type of its array structure *)
  8235. ELSIF IsArrayStructuredObjectType(this) THEN
  8236. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8237. ELSE
  8238. result := FALSE;
  8239. END;
  8240. ELSIF to IS SyntaxTree.StringType THEN
  8241. result := FALSE;
  8242. ELSIF to IS SyntaxTree.EnumerationType THEN
  8243. result := IsEnumerationExtension(this,to);
  8244. ELSIF to IS SyntaxTree.PortType THEN
  8245. result := SameType(to, this)
  8246. ELSE
  8247. Printout.Info("CompatibleTo",to);
  8248. HALT(100); (* implement missing type check *)
  8249. END;
  8250. END;
  8251. RETURN result
  8252. END CompatibleTo;
  8253. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8254. VAR actualBase, formalBase: SyntaxTree.Type;
  8255. BEGIN
  8256. IF SameType(formal,actual) THEN
  8257. RETURN TRUE
  8258. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8259. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8260. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8261. RETURN
  8262. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8263. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8264. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8265. & StaticArrayCompatible(formalBase,actualBase)
  8266. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8267. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8268. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8269. RETURN
  8270. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8271. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8272. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8273. & StaticArrayCompatible(formalBase,actualBase)
  8274. ELSE RETURN FALSE
  8275. END;
  8276. END StaticArrayCompatible;
  8277. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8278. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8279. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8280. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8281. BEGIN
  8282. result := SameType(formal,actual);
  8283. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8284. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8285. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8286. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8287. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8288. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8289. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8290. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8291. & TC(formalBase, actualBase);
  8292. END;
  8293. RETURN result
  8294. END TC;
  8295. BEGIN
  8296. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8297. ELSE
  8298. arrayBase := formalType.arrayBase.resolved;
  8299. IF (actualType IS SyntaxTree.StringType) THEN
  8300. result := arrayBase IS SyntaxTree.CharacterType
  8301. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8302. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8303. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8304. result := TC(formalType, actualType);
  8305. ELSE
  8306. result := (arrayBase IS SyntaxTree.ByteType)
  8307. END;
  8308. END;
  8309. RETURN result
  8310. END OpenArrayCompatible;
  8311. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8312. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8313. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8314. BEGIN
  8315. IF actualType IS SyntaxTree.MathArrayType THEN
  8316. actualArray := actualType(SyntaxTree.MathArrayType);
  8317. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8318. (*
  8319. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8320. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8321. *)
  8322. actualBase := ArrayBase(actualType,Infinity);
  8323. formalBase := ArrayBase(formalType,Infinity);
  8324. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8325. ELSE
  8326. (*
  8327. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8328. *)
  8329. formalBase := Resolved(formalType.arrayBase);
  8330. actualBase := Resolved(actualArray.arrayBase);
  8331. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8332. (*
  8333. ARRAY [k] -> ARRAY [n]
  8334. *)
  8335. result := (formalType.staticLength = actualArray.staticLength)
  8336. ELSE
  8337. result := TRUE
  8338. END;
  8339. IF ~result THEN
  8340. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8341. ELSIF actualBase = NIL THEN result := FALSE
  8342. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8343. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8344. ELSE
  8345. result := SameType(formalBase,actualBase)
  8346. END;
  8347. END;
  8348. ELSE
  8349. result := FALSE
  8350. END;
  8351. RETURN result
  8352. END MathArrayCompatible;
  8353. (**
  8354. Math Array Type distance for assignments / parameter passings of the form
  8355. from -> to
  8356. variants:
  8357. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8358. allowed:
  8359. static -> static (& size match)
  8360. static -> open
  8361. static -> tensor
  8362. open -> open
  8363. open -> tensor
  8364. open -> static
  8365. tensor -> tensor
  8366. tensor -> open
  8367. tensor -> static
  8368. **)
  8369. (*! think about the metric here: is form matching more important than element type matching? *)
  8370. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8371. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8372. BEGIN
  8373. fromBase := Resolved(from.arrayBase);
  8374. toBase := Resolved(to.arrayBase);
  8375. i := Infinity;
  8376. IF from = to THEN
  8377. i := 0;
  8378. ELSIF (from.form = to.form) THEN
  8379. (* static -> static, open -> open, tensor -> tensor *)
  8380. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8381. IF fromBase = toBase THEN i := 0
  8382. ELSIF toBase = NIL THEN i := 1
  8383. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8384. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8385. ELSE
  8386. i := TypeDistance(system,fromBase, toBase, varpar);
  8387. END;
  8388. END;
  8389. ELSIF (to.form = SyntaxTree.Static) THEN
  8390. (* forbidden *)
  8391. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8392. (* static -> tensor, open -> tensor, tensor -> open *)
  8393. IF toBase=fromBase THEN i := 0;
  8394. ELSIF toBase = NIL THEN i := 1;
  8395. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8396. toBase := ArrayBase(toBase,Infinity);
  8397. IF (fromBase=toBase) THEN i := 0
  8398. ELSIF (toBase = NIL) THEN i:= 1
  8399. ELSIF (fromBase = NIL) THEN i := Infinity;
  8400. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8401. END;
  8402. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8403. fromBase := ArrayBase(fromBase,Infinity);
  8404. IF (fromBase=toBase) THEN i := 0
  8405. ELSIF (toBase = NIL) THEN i := 1
  8406. ELSIF (fromBase = NIL) THEN i := Infinity;
  8407. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8408. END;
  8409. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8410. END;
  8411. IF i # Infinity THEN INC(i,2) END;
  8412. ELSIF (from.form = SyntaxTree.Static) THEN
  8413. (* static -> open *)
  8414. IF toBase=fromBase THEN i := 0
  8415. ELSIF toBase = NIL THEN i := 1
  8416. ELSIF fromBase = NIL THEN i := Infinity
  8417. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8418. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8419. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8420. END;
  8421. IF i # Infinity THEN INC(i,1) END;
  8422. ELSE HALT(100); (* unknown case *)
  8423. END;
  8424. RETURN i;
  8425. END MathArrayTypeDistance;
  8426. (** compute and return the distance of two array types
  8427. - return the distance of the base types
  8428. **)
  8429. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8430. VAR i: LONGINT;
  8431. BEGIN
  8432. i := Infinity;
  8433. IF from = to THEN
  8434. i := 0
  8435. ELSE
  8436. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8437. (*
  8438. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8439. i := TypeDistance(from.base, to.base);
  8440. IF i >= 0 THEN INC(i) END
  8441. ELSIF (from.mode = open) & (to.mode = open) THEN
  8442. i := TypeDistance(from.base, to.base);
  8443. *)
  8444. END;
  8445. RETURN i
  8446. END ArrayTypeDistance;
  8447. (** compute the signature distance of a procedure and an actual parameter list
  8448. - if any of the parameters are not compatible, the result is infinite
  8449. - add up and return the distance over all parameters
  8450. **)
  8451. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8452. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8453. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8454. BEGIN
  8455. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8456. result := Infinity
  8457. ELSE
  8458. formalParameter := procedureType.firstParameter;
  8459. i := 0;
  8460. result := 0;
  8461. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8462. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8463. actualParameter := actualParameters.GetExpression(i);
  8464. ASSERT(formalParameter.type # NIL);
  8465. IF (actualParameter.type = NIL) THEN distance := Infinity
  8466. ELSE
  8467. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8468. END;
  8469. IF distance = Infinity THEN
  8470. result := Infinity;
  8471. ELSE
  8472. to := formalParameter.type.resolved;
  8473. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8474. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8475. (* already handled varpar *)
  8476. (*
  8477. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8478. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8479. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8480. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8481. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8482. END;
  8483. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8484. result := Infinity
  8485. END;
  8486. *)
  8487. INC(result, distance);
  8488. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8489. INC(result, distance);
  8490. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8491. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8492. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8493. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8494. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8495. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8496. END;
  8497. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8498. result := Infinity
  8499. END;
  8500. ELSE
  8501. result := Infinity
  8502. END;
  8503. ELSE
  8504. INC(result,distance);
  8505. END;
  8506. END;
  8507. (*
  8508. Printout.Info("actual=", actualParameter);
  8509. Printout.Info("formal=", formalParameter);
  8510. TRACE(result);
  8511. *)
  8512. formalParameter := formalParameter.nextParameter; INC(i);
  8513. END;
  8514. END;
  8515. ASSERT(result >= 0);
  8516. RETURN result
  8517. END Distance;
  8518. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8519. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8520. BEGIN
  8521. IF right.numberParameters # (procedureType.numberParameters) THEN
  8522. result := Infinity
  8523. ELSE
  8524. formalParameter := procedureType.firstParameter;
  8525. rightParameter := right.firstParameter;
  8526. i := 0;
  8527. result := 0;
  8528. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8529. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8530. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8531. IF distance = Infinity THEN
  8532. result := Infinity;
  8533. ELSE
  8534. INC(result,distance);
  8535. END;
  8536. formalParameter := formalParameter.nextParameter;
  8537. rightParameter := rightParameter.nextParameter;
  8538. END;
  8539. END;
  8540. ASSERT(result >= 0);
  8541. RETURN result
  8542. END ProcedureTypeDistance;
  8543. (** compute and return the distance between two types, used for computation of signature distance
  8544. from -> to
  8545. **)
  8546. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8547. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8548. BEGIN
  8549. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8550. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8551. END;
  8552. i := Infinity;
  8553. IF from = to THEN
  8554. i := 0
  8555. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8556. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8557. i := Infinity;
  8558. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8559. i := 10;
  8560. ELSIF (from IS SyntaxTree.StringType) THEN
  8561. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8562. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8563. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8564. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8565. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8566. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8567. i := 1
  8568. ELSIF (from IS SyntaxTree.NilType) THEN
  8569. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8570. (*
  8571. ELSIF (from = NoType) THEN
  8572. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8573. *)
  8574. ELSIF (from IS SyntaxTree.BasicType) THEN
  8575. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8576. IF varpar & (i # 0) THEN i := Infinity END;
  8577. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8578. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8579. ELSIF (from IS SyntaxTree.RecordType) THEN
  8580. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8581. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8582. IF to IS SyntaxTree.MathArrayType THEN
  8583. (*
  8584. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8585. i := Infinity;
  8586. ELSE
  8587. *)
  8588. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8589. (*
  8590. END;
  8591. *)
  8592. END
  8593. ELSIF (from IS SyntaxTree.PointerType) THEN
  8594. ptr := from(SyntaxTree.PointerType);
  8595. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8596. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8597. (* ELSE i := TypeDistance(ptr.base, to); *)
  8598. END
  8599. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8600. IF (to IS SyntaxTree.ProcedureType) THEN
  8601. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8602. END;
  8603. ELSIF (from IS SyntaxTree.PortType) THEN
  8604. IF (to IS SyntaxTree.PortType) THEN
  8605. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8606. i := 0;
  8607. END;
  8608. END;
  8609. (*no procedure test, procedure must be the same*)
  8610. END;
  8611. RETURN i
  8612. END TypeDistance;
  8613. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8614. BEGIN
  8615. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8616. END IsIntegerType;
  8617. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8618. BEGIN
  8619. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8620. END IsAddressType;
  8621. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8622. BEGIN
  8623. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8624. END IsSizeType;
  8625. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8626. BEGIN
  8627. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8628. END IsSignedIntegerType;
  8629. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8630. BEGIN
  8631. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8632. END IsUnsignedIntegerType;
  8633. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8634. VAR result: BOOLEAN;
  8635. BEGIN
  8636. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8637. value := x.resolved(SyntaxTree.IntegerValue).value;
  8638. result := TRUE
  8639. ELSE
  8640. result := FALSE
  8641. END;
  8642. RETURN result
  8643. END IsIntegerValue;
  8644. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8645. VAR result: BOOLEAN;
  8646. BEGIN
  8647. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8648. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8649. result := TRUE
  8650. ELSE
  8651. result := FALSE
  8652. END;
  8653. RETURN result
  8654. END IsEnumerationValue;
  8655. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8656. VAR result: BOOLEAN;
  8657. BEGIN
  8658. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8659. value := x.resolved(SyntaxTree.RealValue).value;
  8660. result := TRUE
  8661. ELSE
  8662. result := FALSE
  8663. END;
  8664. RETURN result
  8665. END IsRealValue;
  8666. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8667. VAR result: BOOLEAN;
  8668. BEGIN
  8669. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8670. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8671. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8672. result := TRUE
  8673. ELSE
  8674. result := FALSE
  8675. END;
  8676. RETURN result
  8677. END IsComplexValue;
  8678. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8679. VAR result: BOOLEAN;
  8680. BEGIN
  8681. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8682. value := x.resolved(SyntaxTree.CharacterValue).value;
  8683. result := TRUE
  8684. ELSE
  8685. result := FALSE
  8686. END;
  8687. RETURN result
  8688. END IsCharacterValue;
  8689. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8690. VAR result: BOOLEAN;
  8691. BEGIN
  8692. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8693. value := x.resolved(SyntaxTree.BooleanValue).value;
  8694. result := TRUE
  8695. ELSE
  8696. result := FALSE
  8697. END;
  8698. RETURN result
  8699. END IsBooleanValue;
  8700. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8701. VAR result: BOOLEAN;
  8702. BEGIN
  8703. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8704. value := x.resolved(SyntaxTree.SetValue).value;
  8705. result := TRUE
  8706. ELSE
  8707. result := FALSE
  8708. END;
  8709. RETURN result
  8710. END IsSetValue;
  8711. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8712. VAR result: BOOLEAN;
  8713. BEGIN
  8714. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8715. value := x.resolved(SyntaxTree.StringValue).value;
  8716. result := TRUE
  8717. ELSE
  8718. result := FALSE
  8719. END;
  8720. RETURN result
  8721. END IsStringValue;
  8722. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8723. BEGIN
  8724. x := x.resolved;
  8725. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8726. END Indexable;
  8727. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8728. BEGIN
  8729. RETURN t1.SameType(t2.resolved);
  8730. END SameType;
  8731. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8732. BEGIN
  8733. IF t IS SyntaxTree.MathArrayType THEN
  8734. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8735. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8736. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8737. DEC(max);
  8738. END;
  8739. ELSIF t IS SyntaxTree.ArrayType THEN
  8740. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8741. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8742. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8743. END;
  8744. END;
  8745. RETURN t;
  8746. END ArrayBase;
  8747. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8748. BEGIN
  8749. type := type.resolved;
  8750. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8751. base := type(SyntaxTree.ArrayType).arrayBase;
  8752. RETURN TRUE;
  8753. END;
  8754. RETURN FALSE;
  8755. END IsOpenArray;
  8756. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8757. BEGIN
  8758. type := type.resolved;
  8759. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8760. base := type(SyntaxTree.ArrayType).arrayBase;
  8761. dim := type(SyntaxTree.ArrayType).staticLength;
  8762. RETURN TRUE
  8763. ELSE
  8764. RETURN FALSE
  8765. END;
  8766. END IsStaticArray;
  8767. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8768. BEGIN
  8769. type := type.resolved;
  8770. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8771. base := type(SyntaxTree.ArrayType).arrayBase;
  8772. RETURN TRUE
  8773. ELSE
  8774. RETURN FALSE
  8775. END;
  8776. END IsDynamicArray;
  8777. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8778. VAR i: LONGINT;
  8779. BEGIN
  8780. i := 0;
  8781. t := t.resolved;
  8782. IF t IS SyntaxTree.MathArrayType THEN
  8783. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8784. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8785. END;
  8786. ELSIF t IS SyntaxTree.ArrayType THEN
  8787. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8788. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8789. END;
  8790. END;
  8791. RETURN i
  8792. END Dimension;
  8793. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8794. BEGIN
  8795. RETURN expression.assignable;
  8796. END IsVariable;
  8797. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8798. BEGIN
  8799. IF (symbol IS SyntaxTree.Parameter) THEN
  8800. WITH symbol: SyntaxTree.Parameter DO
  8801. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8802. END;
  8803. ELSE
  8804. RETURN FALSE
  8805. END;
  8806. END IsVariableParameter;
  8807. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8808. VAR result: BOOLEAN;
  8809. BEGIN
  8810. IF type = NIL THEN result := FALSE
  8811. ELSE
  8812. type := type.resolved;
  8813. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8814. END;
  8815. RETURN result
  8816. END IsPointerType;
  8817. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8818. VAR result: BOOLEAN;
  8819. BEGIN
  8820. IF type = NIL THEN result := FALSE
  8821. ELSE
  8822. type := type.resolved;
  8823. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8824. END;
  8825. RETURN result
  8826. END IsUnsafePointer;
  8827. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8828. BEGIN
  8829. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8830. END IsDisposable;
  8831. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8832. VAR result: BOOLEAN;
  8833. BEGIN
  8834. IF type = NIL THEN result := FALSE
  8835. ELSE
  8836. type := type.resolved;
  8837. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8838. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8839. result := result OR (type IS SyntaxTree.ObjectType);
  8840. END;
  8841. RETURN result
  8842. END IsPointerToRecord;
  8843. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8844. VAR result: BOOLEAN;
  8845. BEGIN
  8846. IF type = NIL THEN result := FALSE
  8847. ELSE
  8848. type := type.resolved;
  8849. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8850. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8851. ;
  8852. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8853. result := result OR (type IS SyntaxTree.ObjectType);
  8854. END;
  8855. RETURN result
  8856. END IsPointerToObject;
  8857. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8858. BEGIN
  8859. IF type # NIL THEN
  8860. RETURN type.resolved.hasPointers
  8861. ELSE
  8862. RETURN FALSE
  8863. END;
  8864. END ContainsPointer;
  8865. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8866. BEGIN
  8867. IF type = NIL THEN RETURN FALSE END;
  8868. type := type.resolved;
  8869. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8870. END IsStringType;
  8871. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8872. BEGIN
  8873. IF type = NIL THEN RETURN FALSE END;
  8874. type := type.resolved;
  8875. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8876. END IsCharacterType;
  8877. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8878. BEGIN
  8879. IF type = NIL THEN RETURN FALSE END;
  8880. type := type.resolved;
  8881. RETURN (type IS SyntaxTree.EnumerationType)
  8882. END IsEnumerationType;
  8883. (** cf. section "Type extension (base type)" in the language report **)
  8884. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8885. VAR result: BOOLEAN;
  8886. BEGIN
  8887. ASSERT(base # NIL); ASSERT(extension # NIL);
  8888. base := base.resolved; extension := extension.resolved;
  8889. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8890. result := TRUE;
  8891. ELSE
  8892. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8893. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8894. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8895. END;
  8896. WHILE (extension # NIL) & (extension # base) DO
  8897. IF extension IS SyntaxTree.RecordType THEN
  8898. extension := extension(SyntaxTree.RecordType).baseType;
  8899. IF (extension # NIL) THEN extension := extension.resolved END;
  8900. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8901. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8902. END;
  8903. ELSE extension := NIL;
  8904. END;
  8905. END;
  8906. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8907. END;
  8908. RETURN result
  8909. END IsTypeExtension;
  8910. (** check if base is the base enumeration type of extension **)
  8911. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8912. BEGIN
  8913. base := base.resolved; extension := extension.resolved;
  8914. WHILE (extension # NIL) & (extension # base) DO
  8915. IF extension IS SyntaxTree.EnumerationType THEN
  8916. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8917. IF extension # NIL THEN extension := extension.resolved END;
  8918. ELSE
  8919. extension := NIL
  8920. END;
  8921. END;
  8922. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8923. END IsEnumerationExtension;
  8924. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8925. BEGIN
  8926. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8927. RETURN TRUE
  8928. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8929. RETURN TRUE
  8930. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8931. RETURN TRUE
  8932. ELSE
  8933. RETURN FALSE
  8934. END
  8935. END IsCallable;
  8936. (** compute and return the distance of two record types
  8937. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8938. **)
  8939. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8940. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8941. BEGIN
  8942. i := 0;
  8943. WHILE (from # NIL) & (from # to) DO
  8944. baseType := from.baseType;
  8945. IF (baseType # NIL) THEN
  8946. baseType := baseType.resolved;
  8947. IF baseType IS SyntaxTree.PointerType THEN
  8948. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8949. END;
  8950. IF baseType IS SyntaxTree.RecordType THEN
  8951. from := baseType(SyntaxTree.RecordType);
  8952. ELSE
  8953. from := NIL;
  8954. END;
  8955. ELSE
  8956. from := NIL
  8957. END;
  8958. INC(i)
  8959. END;
  8960. IF from = NIL THEN i := Infinity END;
  8961. RETURN i
  8962. END RecordTypeDistance;
  8963. (** compute and return the distance of two pointer types **)
  8964. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8965. BEGIN
  8966. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8967. RETURN Infinity;
  8968. ELSE
  8969. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8970. END;
  8971. END PointerTypeDistance;
  8972. (** check if expression contains a symbol designator pointing to a type declaration.
  8973. - if so then enter type declaration into typeDeclaration and return true else return false
  8974. **)
  8975. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8976. VAR result: BOOLEAN;
  8977. BEGIN
  8978. result := FALSE;
  8979. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8980. result := TRUE;
  8981. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8982. END;
  8983. RETURN result
  8984. END IsTypeDesignator;
  8985. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8986. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8987. VAR result: BOOLEAN;
  8988. BEGIN
  8989. type := type.resolved;
  8990. IF type IS SyntaxTree.PointerType THEN
  8991. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8992. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8993. result := TRUE
  8994. ELSE
  8995. result := type IS SyntaxTree.RecordType
  8996. END;
  8997. RETURN result
  8998. END IsExtensibleType;
  8999. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9000. BEGIN
  9001. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9002. (d IS SyntaxTree.SymbolDesignator) &
  9003. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9004. OR
  9005. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9006. END IsUnextensibleRecord;
  9007. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9008. BEGIN
  9009. IF IsUnextensibleRecord(d) THEN
  9010. RETURN FALSE
  9011. ELSE RETURN IsExtensibleType(d.type.resolved)
  9012. END;
  9013. END IsExtensibleDesignator;
  9014. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9015. BEGIN
  9016. type := type.resolved;
  9017. IF (type IS SyntaxTree.PointerType) THEN
  9018. RETURN TRUE
  9019. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9020. RETURN TRUE
  9021. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9022. RETURN TRUE
  9023. ELSIF (type IS SyntaxTree.BasicType) THEN
  9024. RETURN TRUE
  9025. END;
  9026. RETURN FALSE
  9027. END IsBasicType;
  9028. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9029. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9030. BEGIN
  9031. baseType := record.baseType;
  9032. IF (baseType # NIL) THEN
  9033. baseType := baseType.resolved;
  9034. IF (baseType IS SyntaxTree.PointerType) THEN
  9035. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9036. END;
  9037. END;
  9038. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9039. recordType := baseType(SyntaxTree.RecordType);
  9040. ELSE
  9041. recordType := NIL;
  9042. END;
  9043. RETURN recordType
  9044. END RecordBase;
  9045. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9046. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9047. BEGIN
  9048. baseRecord := RecordBase(scope.ownerRecord);
  9049. IF baseRecord = NIL THEN RETURN NIL END;
  9050. scope := baseRecord.recordScope;
  9051. procedureType := procedure.type.resolved;
  9052. IF procedure IS SyntaxTree.Operator THEN
  9053. operator := scope.firstOperator;
  9054. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9055. (*
  9056. Printout.Info("not same ",procedureType);
  9057. Printout.Info("with ",operator.type);
  9058. *)
  9059. operator := operator.nextOperator;
  9060. END;
  9061. super := operator;
  9062. ELSE
  9063. super := scope.firstProcedure;
  9064. WHILE (super # NIL) & (super.name # procedure.name) DO
  9065. super := super.nextProcedure;
  9066. END;
  9067. END;
  9068. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9069. RETURN super
  9070. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9071. RETURN super
  9072. ELSE
  9073. RETURN FindSuperProcedure(scope,procedure);
  9074. END;
  9075. END FindSuperProcedure;
  9076. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9077. VAR procedure: SyntaxTree.Procedure;
  9078. BEGIN
  9079. procedure := record.recordScope.constructor;
  9080. IF procedure = NIL THEN
  9081. record := RecordBase(record);
  9082. IF record # NIL THEN
  9083. procedure := GetConstructor(record)
  9084. END;
  9085. END;
  9086. RETURN procedure;
  9087. END GetConstructor;
  9088. (* enter a case into a list of cases in a sorted way and check for collision *)
  9089. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9090. VAR prev,this,new: SyntaxTree.CaseConstant;
  9091. BEGIN
  9092. this := root;
  9093. prev := NIL;
  9094. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9095. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9096. RETURN FALSE
  9097. ELSE
  9098. IF (this # NIL) & (this.min = max+1) THEN
  9099. this.min := min
  9100. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9101. prev.max := min
  9102. ELSE
  9103. NEW(new); new.min := min; new.max := max;
  9104. new.next := this;
  9105. IF prev = NIL THEN
  9106. root := new;
  9107. ELSE
  9108. prev.next := new
  9109. END
  9110. END;
  9111. RETURN TRUE
  9112. END;
  9113. END EnterCase;
  9114. (** generate and return a new checker object, errors are entered into diagnostics **)
  9115. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9116. VAR checker: Checker;
  9117. BEGIN
  9118. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9119. RETURN checker
  9120. END NewChecker;
  9121. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9122. VAR warnings: Warnings;
  9123. BEGIN
  9124. NEW(warnings, diagnostics); RETURN warnings;
  9125. END NewWarnings;
  9126. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9127. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9128. END IsRangeType;
  9129. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9130. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9131. END IsMathArrayType;
  9132. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9133. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9134. END IsArrayType;
  9135. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9136. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9137. END IsComplexType;
  9138. (** if a type is an array-structured object type *)
  9139. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9140. VAR recordType: SyntaxTree.RecordType;
  9141. BEGIN
  9142. IF type = NIL THEN
  9143. RETURN FALSE
  9144. ELSE
  9145. type := type.resolved;
  9146. IF type IS SyntaxTree.PointerType THEN
  9147. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9148. IF type IS SyntaxTree.RecordType THEN
  9149. recordType := type(SyntaxTree.RecordType);
  9150. RETURN recordType.isObject & recordType.HasArrayStructure()
  9151. ELSE
  9152. RETURN FALSE
  9153. END
  9154. ELSE
  9155. RETURN FALSE
  9156. END
  9157. END
  9158. END IsArrayStructuredObjectType;
  9159. (** the math array structure of a type
  9160. - for math arrays: the array itself
  9161. - for pointers: the math array structure of the pointer base
  9162. - for array-structured object types: the underlying structure
  9163. - for non-math arrays and all other types: NIL
  9164. **)
  9165. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9166. VAR
  9167. result: SyntaxTree.MathArrayType;
  9168. BEGIN
  9169. IF type = NIL THEN
  9170. result := NIL
  9171. ELSE
  9172. type := type.resolved;
  9173. IF type IS SyntaxTree.PointerType THEN
  9174. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9175. END;
  9176. IF type IS SyntaxTree.MathArrayType THEN
  9177. result := type(SyntaxTree.MathArrayType)
  9178. ELSIF type IS SyntaxTree.RecordType THEN
  9179. result := type(SyntaxTree.RecordType).arrayStructure
  9180. ELSE
  9181. result := NIL
  9182. END
  9183. END;
  9184. RETURN result
  9185. END MathArrayStructureOfType;
  9186. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9187. VAR
  9188. result: BOOLEAN;
  9189. rangeExpression: SyntaxTree.RangeExpression;
  9190. BEGIN
  9191. IF x IS SyntaxTree.RangeExpression THEN
  9192. rangeExpression := x(SyntaxTree.RangeExpression);
  9193. result := TRUE;
  9194. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9195. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9196. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9197. ELSE
  9198. result := FALSE
  9199. END;
  9200. RETURN result
  9201. END IsStaticRange;
  9202. (** whether a type is a math array of tensor form **)
  9203. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9204. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9205. END IsTensor;
  9206. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9207. BEGIN
  9208. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9209. length := type(SyntaxTree.MathArrayType).staticLength;
  9210. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9211. RETURN TRUE
  9212. ELSE
  9213. RETURN FALSE
  9214. END;
  9215. END IsStaticMathArray;
  9216. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9217. BEGIN
  9218. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9219. END SymbolHasAddress;
  9220. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9221. BEGIN
  9222. RETURN
  9223. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9224. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9225. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9226. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9227. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9228. ;
  9229. END HasAddress;
  9230. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9231. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9232. BEGIN
  9233. IF (e IS SyntaxTree.Designator) THEN
  9234. d := e(SyntaxTree.Designator);
  9235. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9236. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9237. e := d.left;
  9238. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9239. END;
  9240. IF d # NIL THEN
  9241. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9242. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9243. END;
  9244. END;
  9245. RETURN FALSE;
  9246. END IsLocalVariable;
  9247. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9248. BEGIN
  9249. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9250. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9251. ELSE
  9252. RETURN TRUE
  9253. END;
  9254. END IsStaticProcedure;
  9255. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9256. CONST OptimizeMethodTable = FALSE;
  9257. BEGIN
  9258. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9259. END InMethodTable;
  9260. END FoxSemanticChecker.
  9261. SystemTools.FreeDownTo FoxSemanticChecker ~