FoxSemanticChecker.Mod 403 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192
  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. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  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. cooperative: BOOLEAN;
  66. error-: BOOLEAN;
  67. VerboseErrorMessage: BOOLEAN;
  68. typeFixes, pointerFixes: LateFixList;
  69. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  70. arrayBaseImported: BOOLEAN;
  71. complexNumbersImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. inConversion: LONGINT;
  77. (* temporary variables for the visitors
  78. they replace variables on a stack during use of the visitor pattern and may only be
  79. - set in AcceptXXX procedures
  80. - set and read in ResolveXXX procedures
  81. *)
  82. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  83. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  84. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  85. currentScope-: SyntaxTree.Scope;
  86. currentIsRealtime: BOOLEAN;
  87. currentIsUnreachable: BOOLEAN;
  88. currentIsCellNet: BOOLEAN;
  89. currentIsBodyProcedure: BOOLEAN;
  90. currentIsExclusive: BOOLEAN;
  91. global: SyntaxTree.ModuleScope;
  92. withEntries: WithEntry;
  93. activeCellsStatement: BOOLEAN;
  94. replacements*: Replacement;
  95. cellsAreObjects: BOOLEAN;
  96. variableAccessed: BOOLEAN;
  97. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  98. BEGIN
  99. SELF.diagnostics := diagnostics;
  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. complexNumbersImported := FALSE;
  114. SELF.VerboseErrorMessage := verboseErrorMessage;
  115. global := NIL;
  116. phase := UndefinedPhase;
  117. currentIsRealtime := FALSE;
  118. currentIsUnreachable := FALSE;
  119. currentIsCellNet := FALSE;
  120. currentIsBodyProcedure := FALSE;
  121. currentIsExclusive := FALSE;
  122. withEntries := NIL;
  123. SELF.cellsAreObjects := system.cellsAreObjects;
  124. COPY(backend, backendName);
  125. inConversion := 0;
  126. END InitChecker;
  127. (** report error **)
  128. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  129. VAR errModule: SyntaxTree.Module;
  130. BEGIN
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. Basic.ErrorC(diagnostics, errModule.sourceName, position, Basic.InvalidCode, message);
  134. error := TRUE;
  135. END Error;
  136. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  137. VAR errModule: SyntaxTree.Module;
  138. BEGIN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  141. END Warning;
  142. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  143. VAR errorMessage: ARRAY 256 OF CHAR;
  144. BEGIN
  145. Basic.Concat(errorMessage,msg," ", msg2);
  146. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  147. error := TRUE;
  148. END ErrorSS;
  149. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  150. VAR msg, msg2: ARRAY 256 OF CHAR;
  151. BEGIN
  152. COPY(msg1, msg);
  153. Strings.Append(msg, " = ");
  154. Basic.GetString(s, msg2);
  155. Strings.Append(msg, msg2);
  156. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  157. END InfoSS;
  158. (*** symbol lookup ***)
  159. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  160. **)
  161. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  162. VAR
  163. scope,baseScope: SyntaxTree.Scope;
  164. symbol, s: SyntaxTree.Symbol;
  165. ownerRecord,base: SyntaxTree.RecordType;
  166. BEGIN
  167. scope := inScope;
  168. symbol := NIL;
  169. WHILE (scope # NIL) & (symbol = NIL) DO
  170. symbol := scope.FindSymbol(name);
  171. s := NIL;
  172. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  173. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  175. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. END;
  177. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  178. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  179. ELSE
  180. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  181. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  182. symbol.MarkUsed;
  183. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  184. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  185. base := RecordBase(ownerRecord);
  186. IF (base # NIL) THEN
  187. baseScope := base.recordScope;
  188. symbol := Find(baseScope,name,FALSE);
  189. ELSE
  190. symbol := NIL;
  191. END;
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. END;
  196. END;
  197. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  198. END;
  199. IF (symbol # NIL) THEN
  200. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  201. ASSERT(phase = DeclarationPhase);
  202. ResolveSymbol(symbol)
  203. END;
  204. symbol.MarkUsed;
  205. END;
  206. RETURN symbol
  207. END Find;
  208. (*** types ***)
  209. (** find type declaration with name qualifiedIdentifier and return resolved type
  210. - check qualified identifier prefix, set scope to module scope if appropriate
  211. - check suffix in scope
  212. **)
  213. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  214. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  215. BEGIN
  216. result := NIL;
  217. prevScope := currentScope;
  218. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  219. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  220. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  221. IF symbol(SyntaxTree.Import).module = NIL THEN
  222. Error(qualifiedIdentifier.position,"module not loaded");
  223. result := SyntaxTree.invalidType;
  224. symbol := NIL;
  225. ELSE
  226. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  227. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  228. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  229. IF VerboseErrorMessage THEN
  230. Printout.Info("scope", currentScope);
  231. Printout.Info("symbol", symbol);
  232. END;
  233. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  234. END;
  235. END;
  236. ELSE
  237. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  238. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  239. symbol := NIL;
  240. END;
  241. ELSE
  242. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  243. IF symbol = NIL THEN
  244. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  245. IF VerboseErrorMessage THEN
  246. Printout.Info("Qualident",qualifiedIdentifier);
  247. Printout.Info("in scope",currentScope) ;
  248. END;
  249. END;
  250. END;
  251. IF symbol = NIL THEN (* error already handled *)
  252. typeDeclaration := NIL;
  253. result := SyntaxTree.invalidType;
  254. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  255. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  256. typeDeclaration := NIL;
  257. result := SyntaxTree.invalidType;
  258. ELSE
  259. currentScope := symbol.scope;
  260. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  261. result := ResolveType(typeDeclaration.declaredType);
  262. symbol.MarkUsed;
  263. ASSERT(result # NIL);
  264. END;
  265. currentScope := prevScope;
  266. RETURN result
  267. END ResolveNamedType;
  268. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  269. If node is currently being resolved then emit a cyclic definition error.
  270. Return TRUE only if node is fully resolved.
  271. **)
  272. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  273. VAR result: BOOLEAN;
  274. BEGIN
  275. IF SyntaxTree.Resolved IN x.state THEN
  276. result := FALSE
  277. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  278. Error(x.position,"cyclic definition");
  279. result := FALSE;
  280. ELSE
  281. result := TRUE;
  282. x.SetState(SyntaxTree.BeingResolved)
  283. END;
  284. RETURN result
  285. END TypeNeedsResolution;
  286. (** Return invalid type if x is currently being resolved, return x otherwise**)
  287. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  288. BEGIN
  289. IF SyntaxTree.Resolved IN x.state THEN
  290. RETURN x
  291. ELSE
  292. RETURN SyntaxTree.invalidType
  293. END;
  294. END ResolvedType;
  295. PROCEDURE VisitType*(x: SyntaxTree.Type);
  296. BEGIN
  297. ASSERT(x = SyntaxTree.invalidType);
  298. END VisitType;
  299. (** resolve basic type **)
  300. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  301. BEGIN
  302. IF TypeNeedsResolution(x) THEN
  303. x.SetState(SyntaxTree.Resolved);
  304. END;
  305. resolvedType := ResolvedType(x)
  306. END VisitBasicType;
  307. PROCEDURE VisitByteType*(x: SyntaxTree.ByteType);
  308. BEGIN
  309. VisitBasicType(x);
  310. END VisitByteType;
  311. (** resolve character type **)
  312. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  313. BEGIN
  314. VisitBasicType(x);
  315. END VisitCharacterType;
  316. PROCEDURE VisitBooleanType*(x: SyntaxTree.BooleanType);
  317. BEGIN
  318. VisitBasicType(x);
  319. END VisitBooleanType;
  320. PROCEDURE VisitSetType*(x: SyntaxTree.SetType);
  321. BEGIN
  322. VisitBasicType(x);
  323. END VisitSetType;
  324. PROCEDURE VisitAddressType*(x: SyntaxTree.AddressType);
  325. BEGIN
  326. VisitBasicType(x);
  327. END VisitAddressType;
  328. PROCEDURE VisitSizeType*(x: SyntaxTree.SizeType);
  329. BEGIN
  330. VisitBasicType(x);
  331. END VisitSizeType;
  332. PROCEDURE VisitAnyType*(x: SyntaxTree.AnyType);
  333. BEGIN
  334. VisitBasicType(x);
  335. END VisitAnyType;
  336. PROCEDURE VisitObjectType*(x: SyntaxTree.ObjectType);
  337. BEGIN
  338. VisitBasicType(x);
  339. END VisitObjectType;
  340. PROCEDURE VisitNilType*(x: SyntaxTree.NilType);
  341. BEGIN
  342. VisitBasicType(x);
  343. END VisitNilType;
  344. (** resolve integer type **)
  345. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  346. BEGIN
  347. VisitBasicType(x);
  348. END VisitIntegerType;
  349. (** resolve real type **)
  350. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  351. BEGIN
  352. VisitBasicType(x);
  353. END VisitFloatType;
  354. (** resolve complex type **)
  355. PROCEDURE VisitComplexType*(x: SyntaxTree.ComplexType);
  356. BEGIN
  357. VisitBasicType(x);
  358. END VisitComplexType;
  359. (**
  360. resolve string type: nothing to be done
  361. **)
  362. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  363. BEGIN
  364. IF TypeNeedsResolution(x) THEN
  365. x.SetState(SyntaxTree.Resolved);
  366. END;
  367. resolvedType := ResolvedType(x)
  368. END VisitStringType;
  369. (**
  370. check enumeration scope: enter symbols and check for duplicate names
  371. **)
  372. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: Basic.Integer);
  373. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: Basic.Integer; prevScope: SyntaxTree.Scope;
  374. BEGIN
  375. prevScope := currentScope;
  376. currentScope := x;
  377. e := x.firstConstant;
  378. nextHighest := highest;
  379. WHILE (e # NIL) DO
  380. Register(e,x,FALSE);
  381. IF SymbolNeedsResolution(e) THEN
  382. IF e.value # NIL THEN
  383. value := ConstantExpression(e.value);
  384. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  385. ELSE
  386. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  387. value.SetType(x.ownerEnumeration);
  388. END;
  389. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  390. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  391. IF nextHighest > highest THEN highest := nextHighest END;
  392. END;
  393. e.SetValue(value);
  394. CheckSymbolVisibility(e);
  395. e.SetType(x.ownerEnumeration);
  396. e.SetState(SyntaxTree.Resolved);
  397. END;
  398. e := e.nextConstant;
  399. END;
  400. currentScope := prevScope;
  401. END CheckEnumerationScope;
  402. (**
  403. resolve enumeration type: check enumeration scope
  404. **)
  405. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  406. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  407. lowest, highest: Basic.Integer;
  408. BEGIN
  409. IF TypeNeedsResolution(x) THEN
  410. IF x.enumerationBase # NIL THEN
  411. position := x.enumerationBase.position;
  412. baseType := ResolveType(x.enumerationBase);
  413. resolved := baseType.resolved;
  414. baseScope := NIL;
  415. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  416. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  417. Error(position, "base type is no enumeration type");
  418. ELSE
  419. enumerationBase := resolved(SyntaxTree.EnumerationType);
  420. lowest := enumerationBase.rangeHighest+1;
  421. END;
  422. x.SetEnumerationBase(baseType);
  423. ELSE lowest := 0;
  424. END;
  425. highest := lowest-1;
  426. CheckEnumerationScope(x.enumerationScope, highest);
  427. x.SetRange(lowest, highest);
  428. x.SetState(SyntaxTree.Resolved);
  429. END;
  430. resolvedType := ResolvedType(x);
  431. END VisitEnumerationType;
  432. (**
  433. resolve range type: nothing to be done
  434. **)
  435. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  436. BEGIN
  437. IF TypeNeedsResolution(x) THEN
  438. x.SetState(SyntaxTree.Resolved);
  439. END;
  440. resolvedType := ResolvedType(x)
  441. END VisitRangeType;
  442. (**
  443. resolve qualified type
  444. - find and resolve named type and set resolved type
  445. **)
  446. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  447. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  448. BEGIN
  449. IF TypeNeedsResolution(x) THEN
  450. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  451. x.SetResolved(type.resolved);
  452. x.SetState(SyntaxTree.Resolved);
  453. x.SetTypeDeclaration (typeDeclaration);
  454. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  455. x.SetResolved(SyntaxTree.invalidType);
  456. END;
  457. resolvedType := x;
  458. END VisitQualifiedType;
  459. (**
  460. resolve array type
  461. - check base type
  462. - array of math array forbidden
  463. - static array of open array forbidden
  464. **)
  465. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  466. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  467. BEGIN
  468. IF TypeNeedsResolution(x) THEN
  469. x.SetArrayBase(ResolveType(x.arrayBase));
  470. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  471. arrayBase := x.arrayBase.resolved;
  472. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  473. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  474. pointerType.SetPointerBase(arrayBase);
  475. pointerType.SetHidden(TRUE);
  476. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  477. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  478. ELSE
  479. x.SetArrayBase(pointerType);
  480. END;
  481. END;
  482. IF x.length # NIL THEN
  483. variableAccessed := FALSE;
  484. e := ResolveExpression(x.length);
  485. IF (e.resolved = NIL) THEN
  486. IF variableAccessed THEN
  487. Error(e.position, "forbidden variable access");
  488. END;
  489. IF CheckSizeType(e) THEN
  490. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  491. END;
  492. ELSE
  493. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  494. END;
  495. END;
  496. IF arrayBase IS SyntaxTree.ArrayType THEN
  497. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  498. Error(x.position,"forbidden static array of dynamic array");
  499. END;
  500. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  501. Error(x.position,"forbidden array mixed form");
  502. END;
  503. x.SetHasPointers(arrayBase.hasPointers);
  504. x.SetState(SyntaxTree.Resolved);
  505. END;
  506. resolvedType := ResolvedType(x);
  507. END VisitArrayType;
  508. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  509. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  510. BEGIN
  511. module := currentScope.ownerModule;
  512. IF module.name=name THEN
  513. (* do nothing *)
  514. ELSE
  515. moduleScope := module.moduleScope;
  516. import := moduleScope.FindImport(name);
  517. IF import = NIL THEN
  518. import := SyntaxTree.NewImport(position,name,name,TRUE);
  519. moduleScope.AddImport(import);
  520. Register(import,moduleScope,FALSE);
  521. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  522. VisitImport(import);
  523. ELSIF import.direct=FALSE THEN
  524. import.SetScope(module.moduleScope);
  525. import.SetDirect(TRUE);
  526. IF moduleScope.FindSymbol(import.name) = NIL THEN
  527. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  528. duplicate.SetContext(import.context);
  529. duplicate.SetModule(import.module);
  530. Register(duplicate,moduleScope,TRUE);
  531. VisitImport(duplicate);
  532. END;
  533. END;
  534. import.MarkUsed
  535. END;
  536. END ImportModule;
  537. (**
  538. resolve math array type
  539. - check base type
  540. - open math array of array forbidden
  541. - math array of tensor forbidden
  542. - static array of open array forbidden
  543. **)
  544. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  545. VAR arrayBase: SyntaxTree.Type; modifiers: SyntaxTree.Modifier; position: SyntaxTree.Position;
  546. BEGIN
  547. IF TypeNeedsResolution(x) THEN
  548. x.SetArrayBase(ResolveType(x.arrayBase));
  549. IF x.length # NIL THEN
  550. x.SetLength(ConstantIntegerGeq0(x.length));
  551. END;
  552. arrayBase := x.arrayBase;
  553. IF arrayBase # NIL THEN
  554. arrayBase := arrayBase.resolved;
  555. IF arrayBase = SyntaxTree.invalidType THEN
  556. (* error already handled *)
  557. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  558. Error(x.position,"forbidden array mixed form");
  559. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  560. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  561. Error(x.position,"forbidden Tensor Array mix")
  562. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  563. Error(x.position,"forbidden static array of dynamic array")
  564. END;
  565. END;
  566. IF x.form = SyntaxTree.Static THEN
  567. x.SetIncrement(system.SizeOf(arrayBase));
  568. END;
  569. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  570. END;
  571. modifiers := x.modifiers;
  572. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  573. CheckModifiers(modifiers, TRUE);
  574. x.SetState(SyntaxTree.Resolved);
  575. END;
  576. resolvedType := ResolvedType(x);
  577. END VisitMathArrayType;
  578. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  579. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  580. (1) Rec = RECORD ... END; Ptr <---> Rec
  581. Ptr = POINTER TO Rec; ^ |
  582. | |
  583. TypeDesc TypeDesc
  584. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  585. ^ /
  586. | /
  587. TypeDesc <-- /
  588. *)
  589. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  590. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  591. BEGIN
  592. Strings.IntToStr(x.position.start,number);
  593. COPY(prefix,name);
  594. Strings.Append(name,"@");
  595. Strings.Append(name,number);
  596. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  597. typeDeclaration.SetDeclaredType(x);
  598. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  599. x.SetTypeDeclaration(typeDeclaration);
  600. currentScope.AddTypeDeclaration(typeDeclaration);
  601. typeDeclaration.SetScope(currentScope);
  602. END AnonymousTypeDeclaration;
  603. (**
  604. deferred pointer type resolving
  605. - resolve base type
  606. - check that base type is a record or array type
  607. - if error then set base type to invalid type
  608. **)
  609. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  610. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  611. BEGIN
  612. ASSERT(type.pointerBase # NIL);
  613. position := type.pointerBase.position;
  614. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  615. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  616. (* not for pointers, a type is needed for the records only
  617. IF type.typeDeclaration = NIL THEN
  618. AnonymousTypeDeclaration(type);
  619. END;
  620. *)
  621. END;
  622. resolved := ResolveType(type.pointerBase);
  623. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  624. type.SetPointerBase(resolved);
  625. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  626. recordType := resolved.resolved(SyntaxTree.RecordType);
  627. IF recordType.isObject & (recordType.baseType # NIL) THEN
  628. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  629. Error(position, "base type of object must be a realtime object");
  630. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  631. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  632. END;
  633. END;
  634. END;
  635. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  636. Error(position, "realtime object contains references to non-realtime objects");
  637. END;
  638. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  639. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  640. Error(position, "forbidden unsafe at static array");
  641. ELS
  642. *)
  643. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  644. Error(position, "forbidden unsafe at multidimensional array");
  645. END;
  646. END;
  647. ELSE
  648. Error(position, "forbidden pointer base type");
  649. type.SetPointerBase(SyntaxTree.invalidType)
  650. END
  651. END FixPointerType;
  652. (**
  653. resolve pointer type
  654. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  655. **)
  656. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  657. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  658. modifiers: SyntaxTree.Modifier; position: Position;
  659. BEGIN
  660. IF TypeNeedsResolution(x) THEN
  661. modifiers := x.modifiers;
  662. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  663. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  664. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  665. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  666. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,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. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  679. END;
  680. CheckModifiers(modifiers, TRUE);
  681. typeFixes.Add(x,currentScope);
  682. x.SetState(SyntaxTree.Resolved);
  683. END;
  684. resolvedType := ResolvedType(x)
  685. END VisitPointerType;
  686. (**
  687. resolve port type
  688. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  689. **)
  690. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  691. VAR value: Basic.Integer;
  692. BEGIN
  693. IF TypeNeedsResolution(x) THEN
  694. x.SetCellsAreObjects(cellsAreObjects);
  695. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  696. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  697. x.SetSize(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  698. ELSE
  699. x.SetSize(system.SizeOf(system.longintType));
  700. END;
  701. x.SetState(SyntaxTree.Resolved);
  702. END;
  703. resolvedType := ResolvedType(x)
  704. END VisitPortType;
  705. (**
  706. deferred procedure type resolving
  707. - resolve return type
  708. - traverse and resolve parameters
  709. **)
  710. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  711. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  712. BEGIN
  713. resolved := ResolveType(procedureType.returnType);
  714. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  715. Error(procedureType.position,"forbidden open array return type");
  716. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  717. Error(procedureType.position,"procedure with return type does not return");
  718. END;
  719. procedureType.SetReturnType(resolved);
  720. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  721. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  722. parameter.SetType(procedureType.returnType);
  723. parameter.SetAccess(SyntaxTree.Hidden);
  724. parameter.SetUntraced(procedureType.hasUntracedReturn);
  725. VisitParameter(parameter);
  726. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  727. END;
  728. (* process parameters *)
  729. parameter :=procedureType.firstParameter;
  730. WHILE (parameter # NIL) DO
  731. VisitParameter(parameter);
  732. parameter := parameter.nextParameter;
  733. END;
  734. parameter := procedureType.selfParameter;
  735. IF parameter # NIL THEN
  736. VisitParameter(parameter)
  737. END;
  738. END FixProcedureType;
  739. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  740. VAR prev,this: SyntaxTree.Modifier;
  741. BEGIN
  742. this := modifiers;prev := NIL;
  743. WHILE (this # NIL) & (this.identifier # name) DO
  744. prev := this; this := this.nextModifier;
  745. END;
  746. IF this # NIL THEN
  747. IF this.expression # NIL THEN
  748. Error(this.position,"unexpected expression");
  749. END;
  750. this.Resolved;
  751. position := this.position;
  752. RETURN TRUE
  753. ELSE
  754. RETURN FALSE
  755. END;
  756. END HasFlag;
  757. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: Basic.Integer): BOOLEAN;
  758. VAR prev,this: SyntaxTree.Modifier;
  759. BEGIN
  760. this := modifiers;prev := NIL;
  761. WHILE (this # NIL) & (this.identifier # name) DO
  762. prev := this; this := this.nextModifier;
  763. END;
  764. IF this # NIL THEN
  765. IF this.expression = NIL THEN
  766. Error(this.position,"expected expression value");
  767. ELSE
  768. this.SetExpression(ConstantExpression(this.expression));
  769. IF CheckIntegerValue(this.expression,value) THEN END;
  770. END;
  771. this.Resolved;
  772. position := this.position;
  773. RETURN TRUE
  774. ELSE RETURN FALSE
  775. END;
  776. END HasValue;
  777. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  778. VAR prev,this: SyntaxTree.Modifier;
  779. BEGIN
  780. this := modifiers;prev := NIL;
  781. WHILE (this # NIL) & (this.identifier # name) DO
  782. prev := this; this := this.nextModifier;
  783. END;
  784. IF this # NIL THEN
  785. IF this.expression = NIL THEN
  786. Error(this.position,"expected expression value");
  787. ELSE
  788. this.SetExpression(ConstantExpression(this.expression));
  789. IF CheckStringValue(this.expression,value) THEN END;
  790. END;
  791. this.Resolved;
  792. position := this.position;
  793. RETURN TRUE
  794. ELSE RETURN FALSE
  795. END;
  796. END HasStringValue;
  797. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  798. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  799. BEGIN
  800. IF cellsAreObjects THEN RETURN FALSE END;
  801. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  802. IF svalue = "A2" THEN
  803. RETURN TRUE
  804. END;
  805. END;
  806. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  807. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  808. END;
  809. RETURN FALSE;
  810. (*
  811. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  812. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  813. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  814. IF svalue[0] = "~" THEN
  815. Strings.TrimLeft(svalue, "~");
  816. IF svalue = backendName THEN
  817. RETURN TRUE;
  818. END;
  819. ELSIF svalue # backendName THEN
  820. RETURN TRUE;
  821. END;
  822. END;
  823. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  824. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  825. END;
  826. RETURN FALSE;
  827. *)
  828. END SkipImplementation;
  829. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  830. VAR this: SyntaxTree.Modifier;
  831. BEGIN
  832. this := modifiers;
  833. WHILE this # NIL DO
  834. IF ~this.resolved THEN
  835. IF checkUse THEN
  836. Error(this.position,"unexpected modifier");
  837. ELSE
  838. this.SetExpression(ResolveExpression(this.expression));
  839. this.Resolved;
  840. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  841. END;
  842. END;
  843. this := this.nextModifier
  844. END;
  845. END CheckModifiers;
  846. (**
  847. resolve procedure type
  848. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  849. **)
  850. PROCEDURE VisitProcedureType*(procedureType: SyntaxTree.ProcedureType);
  851. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position;
  852. BEGIN
  853. IF TypeNeedsResolution(procedureType) THEN
  854. modifiers := procedureType.modifiers;
  855. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  856. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  857. procedureType.SetInterrupt(TRUE);
  858. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  859. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  860. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  861. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  862. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  863. Error(position, "undefined platform calling convention");
  864. ELSE
  865. procedureType.SetCallingConvention(system.platformCallingConvention);
  866. END;
  867. END;
  868. IF HasFlag(modifiers, Global.NameNoReturn,position) THEN procedureType.SetNoReturn(TRUE) END;
  869. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(LONGINT (value)) END; (* TODO: fix explicit integer truncation *)
  870. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  871. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  872. CheckModifiers(modifiers, TRUE);
  873. modifiers := procedureType.returnTypeModifiers;
  874. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  875. CheckModifiers(modifiers, TRUE);
  876. typeFixes.Add(procedureType,currentScope);
  877. procedureType.SetHasPointers(procedureType.isDelegate);
  878. procedureType.SetState(SyntaxTree.Resolved);
  879. END;
  880. resolvedType := ResolvedType(procedureType)
  881. END VisitProcedureType;
  882. (** check and resolve record type
  883. - check base type: must be record, math array or array-structured object type
  884. - check declarations
  885. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  886. **)
  887. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  888. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  889. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  890. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  891. hasPointers: BOOLEAN;
  892. modifiers: SyntaxTree.Modifier;
  893. value: Basic.Integer;
  894. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  895. BEGIN
  896. type := type.resolved;
  897. IF (type IS SyntaxTree.PointerType) &
  898. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  899. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  900. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  901. RETURN TRUE
  902. ELSE
  903. RETURN FALSE
  904. END;
  905. END IsPointerToRecord;
  906. BEGIN
  907. IF TypeNeedsResolution(x) THEN
  908. hasPointers := FALSE;
  909. modifiers := x.modifiers;
  910. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(LONGINT(value)*system.dataUnit) (* TODO: fix explicit integer truncation *)
  911. END;
  912. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  913. CheckModifiers(modifiers, TRUE);
  914. IF x.baseType # NIL THEN
  915. position := x.baseType.position;
  916. baseType := ResolveType(x.baseType);
  917. resolved := baseType.resolved;
  918. hasPointers := hasPointers OR resolved.hasPointers;
  919. IF x.isObject THEN (* object *)
  920. ASSERT(x.pointerType # NIL);
  921. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  922. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  923. baseType := NIL
  924. ELSIF IsPointerToRecord(resolved,recordType) THEN
  925. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  926. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  927. ELSE
  928. Error(position,"object does not extend pointer to record, object or math array ")
  929. END;
  930. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  931. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  932. ELSIF IsPointerToRecord(resolved,recordType) THEN
  933. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  934. ELSIF resolved IS SyntaxTree.RecordType THEN
  935. ELSE
  936. Error(position,"pointer to record does not extend pointer to record or record")
  937. END;
  938. ELSE
  939. IF resolved IS SyntaxTree.RecordType THEN
  940. ELSE
  941. Error(position,"record does not extend record")
  942. END;
  943. END;
  944. x.SetBaseType(baseType);
  945. IF x.Level() > 15 THEN
  946. Error(position, "record/object inheritance level too high");
  947. (* note:
  948. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  949. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  950. inheritance history of a type.
  951. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  952. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  953. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  954. *)
  955. END;
  956. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  957. Error(position, "invalid inheritance of disposable types");
  958. END;
  959. END;
  960. Declarations(x.recordScope, FALSE, {0});
  961. x.SetState(SyntaxTree.Resolved);
  962. Declarations(x.recordScope, FALSE, {1});
  963. ResolveArrayStructure(x);
  964. (* computation of sizes and offsets skipped -> done in backend / system *)
  965. recordBase := x.GetBaseRecord();
  966. IF recordBase = NIL THEN numberMethods := 0
  967. ELSE numberMethods := recordBase.recordScope.numberMethods
  968. END;
  969. isRealtime := TRUE;
  970. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  971. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  972. END;
  973. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  974. WHILE symbol # NIL DO
  975. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  976. IF symbol IS SyntaxTree.Variable THEN
  977. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  978. END;
  979. IF symbol IS SyntaxTree.Procedure THEN
  980. procedure := symbol(SyntaxTree.Procedure);
  981. IF procedure.super # NIL THEN
  982. procedure.SetMethodNumber(procedure.super.methodNumber);
  983. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  984. procedure.SetMethodNumber(numberMethods);
  985. INC(numberMethods);
  986. END;
  987. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  988. Error(procedure.position,"realtime procedure in non-realtime object")
  989. END;
  990. END;
  991. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  992. Error(symbol.position,"non-realtime symbol in realtime object")
  993. END;
  994. symbol := symbol.nextSymbol;
  995. END;
  996. IF isRealtime THEN x.SetRealtime(TRUE) END;
  997. x.recordScope.SetNumberMethods(numberMethods);
  998. (* TODO: is this needed anymore? *)
  999. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  1000. Error(x.position,"object extends a record")
  1001. END;
  1002. IF ~x.isAbstract THEN
  1003. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  1004. Error(x.position, "non-abstract object contains abstract procedure");
  1005. END;
  1006. ELSE
  1007. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  1008. Error(x.position, "abstract object does not contain an abstract method");
  1009. END;
  1010. END;
  1011. IF (x.typeDeclaration = NIL) THEN
  1012. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  1013. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  1014. (*
  1015. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  1016. AnonymousTypeDeclaration(x,name);
  1017. *)
  1018. ELSE
  1019. AnonymousTypeDeclaration(x,"Anonymous");
  1020. END;
  1021. END;
  1022. x.SetHasPointers(hasPointers);
  1023. x.SetState(SyntaxTree.Resolved);
  1024. END;
  1025. resolvedType := ResolvedType(x);
  1026. END VisitRecordType;
  1027. (** check and resolve cell type
  1028. - check base type: must be cell
  1029. - check declarations
  1030. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1031. **)
  1032. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  1033. VAR
  1034. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1035. modifier: SyntaxTree.Modifier; position: Position; value: Basic.Integer; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1036. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1037. recordBase: SyntaxTree.RecordType;
  1038. numberMethods: LONGINT;
  1039. int: Basic.Integer;
  1040. real: LONGREAL;
  1041. bool: BOOLEAN;
  1042. set: Basic.Set;
  1043. v: SyntaxTree.Expression;
  1044. str: Scanner.StringType;
  1045. atype: SyntaxTree.ArrayType;
  1046. prev: SyntaxTree.Scope;
  1047. BEGIN
  1048. IF TypeNeedsResolution(x) THEN
  1049. recordBase := NIL;
  1050. IF cellsAreObjects THEN
  1051. IF x.baseType = NIL THEN
  1052. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1053. ImportModule(qualifiedIdentifier.prefix, x.position);
  1054. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1055. x.SetBaseType(ResolveType(x.baseType));
  1056. recordBase := x.GetBaseRecord();
  1057. IF recordBase = NIL THEN
  1058. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1059. END;
  1060. ELSE
  1061. x.SetBaseType(ResolveType(x.baseType));
  1062. END;
  1063. ELSE
  1064. x.SetBaseType(ResolveType(x.baseType));
  1065. END;
  1066. IF recordBase = NIL THEN numberMethods := 0
  1067. ELSE numberMethods := recordBase.recordScope.numberMethods
  1068. END;
  1069. modifier := x.modifiers;
  1070. (*IF ~x.isCellNet THEN*)
  1071. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1072. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1073. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1074. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1075. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1076. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1077. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1078. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1079. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1080. symbol := system.activeCellsCapabilities;
  1081. WHILE symbol # NIL DO
  1082. IF HasFlag(modifier, symbol.name, position) THEN END;
  1083. symbol := symbol.nextSymbol;
  1084. END;
  1085. modifier := x.modifiers;
  1086. WHILE (modifier # NIL) DO
  1087. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1088. IF modifier.expression # NIL THEN
  1089. v := ConstantExpression(modifier.expression);
  1090. property.SetValue(v);
  1091. IF IsIntegerValue(modifier.expression, int) THEN
  1092. (*property.SetValue(modifier.expression);*)
  1093. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1094. property.SetType(system.longintType);
  1095. ELSIF IsRealValue(modifier.expression, real) THEN
  1096. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1097. property.SetType(system.longrealType);
  1098. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1099. property.SetType(system.booleanType);
  1100. ELSIF IsSetValue(modifier.expression, set) THEN
  1101. property.SetType(system.setType);
  1102. ELSIF IsStringValue(modifier.expression, str) THEN
  1103. (*property.SetValue(modifier.expression);*)
  1104. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1105. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1106. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1107. property.SetType(atype);
  1108. ELSE
  1109. Error(modifier.position, "unsupported property type");
  1110. END;
  1111. ELSE (* flag property *)
  1112. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1113. property.SetType(system.booleanType);
  1114. END;
  1115. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1116. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1117. x.AddProperty(property);
  1118. modifier := modifier.nextModifier;
  1119. END;
  1120. CheckModifiers(modifier, FALSE);
  1121. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1122. (* process parameters *)
  1123. prev := currentScope;
  1124. currentScope := x.cellScope;
  1125. parameter :=x.firstParameter;
  1126. WHILE (parameter # NIL) DO
  1127. VisitParameter(parameter);
  1128. type := parameter.type.resolved;
  1129. IF ~(type IS SyntaxTree.PortType) THEN
  1130. WHILE IsStaticArray(type, type, len) DO
  1131. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1132. END;
  1133. WHILE IsDynamicArray(type, type) DO
  1134. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1135. END;
  1136. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1137. Error(parameter.position, "invalid type, must be port or static array of port ");
  1138. END;
  1139. END;
  1140. parameter := parameter.nextParameter;
  1141. END;
  1142. currentScope := prev;
  1143. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1144. WHILE symbol # NIL DO
  1145. IF symbol IS SyntaxTree.Variable THEN
  1146. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1147. END;
  1148. symbol := symbol.nextSymbol;
  1149. END;
  1150. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1151. IF (x.typeDeclaration = NIL) THEN
  1152. AnonymousTypeDeclaration(x,"Anonymous");
  1153. END;
  1154. x.SetState(SyntaxTree.Resolved);
  1155. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1156. Warning(x.position, "Forbidden empty Body.");
  1157. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1158. Warning(x.position, "Non-empty body for an engine?");
  1159. END;
  1160. END;
  1161. resolvedType := ResolvedType(x);
  1162. END VisitCellType;
  1163. (* check if an object is an array-structured object type
  1164. - determine the array structure
  1165. - collect operators from top to bottom in the inheritance hierarchy
  1166. - check if LEN operator is declared
  1167. - determine number of possible index operators
  1168. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1169. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1170. *)
  1171. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1172. VAR
  1173. indexOperatorCount, i: LONGINT;
  1174. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1175. isTensor: BOOLEAN;
  1176. BEGIN
  1177. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1178. (* determine array structure *)
  1179. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1180. END;
  1181. IF recordType.HasArrayStructure() THEN
  1182. (* the object is an ASOT *)
  1183. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1184. (* reset array access operators *)
  1185. arrayAccessOperators.len := NIL;
  1186. arrayAccessOperators.generalRead := NIL;
  1187. arrayAccessOperators.generalWrite := NIL;
  1188. IF isTensor THEN
  1189. (* all operators of dimensionalities 1 to max *)
  1190. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1191. ELSE
  1192. (* all operators of certain dimensionality *)
  1193. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1194. END;
  1195. NEW(arrayAccessOperators.read, indexOperatorCount);
  1196. NEW(arrayAccessOperators.write, indexOperatorCount);
  1197. FOR i := 0 TO indexOperatorCount - 1 DO
  1198. arrayAccessOperators.read[i] := NIL;
  1199. arrayAccessOperators.write[i] := NIL
  1200. END;
  1201. (* collect access operators in the record scope *)
  1202. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1203. IF arrayAccessOperators.len = NIL THEN
  1204. (* TODO: think about making this operator optional for static array structures *)
  1205. Error(recordType.position, "LEN operator missing")
  1206. END;
  1207. (* show error messages *)
  1208. IF isTensor THEN
  1209. (* require ARRAY [*] OF RANGE *)
  1210. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1211. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1212. ELSE
  1213. (* forbid ARRAY [*] OF RANGE *)
  1214. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1215. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1216. (* require RANGE, RANGE, ... RANGE *)
  1217. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1218. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1219. END;
  1220. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1221. ELSE
  1222. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1223. IF recordType.recordScope.firstOperator # NIL THEN
  1224. RETURN;
  1225. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1226. END
  1227. END
  1228. END ResolveArrayStructure;
  1229. (** collect array access operators in a record scope **)
  1230. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1231. VAR
  1232. baseType: SyntaxTree.Type;
  1233. operator: SyntaxTree.Operator;
  1234. isReadOperator, isGeneralOperator: BOOLEAN;
  1235. indexListSize, indexListKind, hashValue: LONGINT;
  1236. BEGIN
  1237. (* if a parent record scope exists, collect the operators there first *)
  1238. baseType := recordScope.ownerRecord.baseType;
  1239. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1240. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1241. END;
  1242. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1243. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1244. END;
  1245. (* go through all operators in the current record scope *)
  1246. operator := recordScope.firstOperator;
  1247. WHILE operator # NIL DO
  1248. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1249. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1250. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1251. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1252. IF isGeneralOperator THEN
  1253. IF isReadOperator THEN
  1254. arrayAccessOperators.generalRead := operator
  1255. ELSE
  1256. arrayAccessOperators.generalWrite := operator
  1257. END
  1258. ELSE
  1259. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1260. IF isReadOperator THEN
  1261. arrayAccessOperators.read[hashValue] := operator
  1262. ELSE
  1263. arrayAccessOperators.write[hashValue] := operator
  1264. END
  1265. END
  1266. END
  1267. ELSE
  1268. Error(operator.position, 'invalid operator')
  1269. END;
  1270. operator := operator.nextOperator
  1271. END
  1272. END CollectArrayAccessOperators;
  1273. (** the hash value of an index operator **)
  1274. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1275. VAR result: LONGINT;
  1276. BEGIN
  1277. IF isTensor THEN
  1278. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1279. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1280. ELSE
  1281. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1282. END
  1283. ELSE
  1284. result := indexListKind
  1285. END;
  1286. RETURN result
  1287. END IndexOperatorHash;
  1288. (** 2 to the power of exponent **)
  1289. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1290. VAR result, i: LONGINT;
  1291. BEGIN
  1292. result := 1;
  1293. FOR i := 1 TO exponent DO
  1294. result := result * 2;
  1295. END;
  1296. RETURN result
  1297. END TwoToThePowerOf;
  1298. (** check if a LEN operator has a correct signature. i.e.
  1299. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF <LENTYPE>;'
  1300. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF <LENTYPE>;'
  1301. **)
  1302. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1303. VAR
  1304. procedureType: SyntaxTree.ProcedureType;
  1305. returnedArrayType: SyntaxTree.MathArrayType;
  1306. result: BOOLEAN;
  1307. BEGIN
  1308. result := FALSE;
  1309. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1310. IF (procedureType.numberParameters = 0) THEN
  1311. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1312. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1313. IF system.lenType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1314. IF returnedArrayType.form = SyntaxTree.Open THEN
  1315. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1316. result := TRUE
  1317. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1318. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1319. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1320. result := TRUE
  1321. END
  1322. END
  1323. END
  1324. END
  1325. END;
  1326. IF result THEN
  1327. (* export symbol automatically *)
  1328. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1329. ELSE
  1330. Error(operator.position, "LEN operator with invalid signature");
  1331. END;
  1332. RETURN result
  1333. END CheckLenOperator;
  1334. (** check if an index operator has a correct signature. i.e.
  1335. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1336. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1337. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1338. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1339. - determine if it is a read or write operator (existance of return type)
  1340. - check index parameters
  1341. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1342. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1343. [LONGINT] -> binary 0 -> 0
  1344. [RANGE] -> binary 1 -> 1
  1345. [LONGINT, LONGINT] -> binary 00 -> 0
  1346. [LONGINT, RANGE] -> binary 01 -> 1
  1347. [RANGE, LONGINT] -> binary 10 -> 2
  1348. [RANGE, RANGE] -> binary 11 -> 3
  1349. etc.
  1350. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1351. - for read operators, check if return type matches the type of data that is read
  1352. - for write operators, check if last parameter type matches the type of data that is written
  1353. **)
  1354. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1355. VAR
  1356. elementType, otherElementType, dataType: SyntaxTree.Type;
  1357. procedureType: SyntaxTree.ProcedureType;
  1358. mathArrayType: SyntaxTree.MathArrayType;
  1359. parameter: SyntaxTree.Parameter;
  1360. parameterCount, rangeCount, i: LONGINT;
  1361. hasTypeError: BOOLEAN;
  1362. BEGIN
  1363. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1364. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1365. (* determine if it is a read or write operator *)
  1366. isReadOperator := (procedureType.returnType # NIL);
  1367. IF isReadOperator THEN
  1368. indexListSize := parameterCount;
  1369. ELSE
  1370. indexListSize := parameterCount - 1;
  1371. END;
  1372. IF indexListSize < 1 THEN
  1373. Error(operator.position, "index operator with too few parameters");
  1374. RETURN FALSE
  1375. END;
  1376. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1377. (* general operator *)
  1378. isGeneralOperator := TRUE;
  1379. IF indexListSize > 1 THEN
  1380. Error(operator.position, "index operator with too many parameters");
  1381. RETURN FALSE
  1382. END;
  1383. (* ARRAY [*] OF RANGE*)
  1384. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1385. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1386. Error(operator.position, "index parameter not dynamic math array of range");
  1387. RETURN FALSE
  1388. END;
  1389. parameter := procedureType.firstParameter.nextParameter
  1390. ELSE
  1391. (* fixed-dim. operator *)
  1392. isGeneralOperator := FALSE;
  1393. (* check number of index parameters *)
  1394. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1395. (* for tensors, limited to a certain size *)
  1396. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1397. Error(operator.position, "too many index parameters for tensor");
  1398. RETURN FALSE
  1399. END
  1400. ELSE
  1401. (* for non-tensors, depends on dimensionality *)
  1402. IF indexListSize # arrayStructure.Dimensionality() THEN
  1403. Error(operator.position, "index parameter count does not match dimensionality");
  1404. RETURN FALSE
  1405. END
  1406. END;
  1407. (* go through all index parameters
  1408. - count the number of ranges
  1409. - determine the index list kind number
  1410. *)
  1411. indexListKind := 0;
  1412. rangeCount := 0;
  1413. parameter := procedureType.firstParameter;
  1414. FOR i := 1 TO indexListSize DO
  1415. indexListKind := indexListKind * 2;
  1416. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1417. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1418. INC(indexListKind);
  1419. INC(rangeCount)
  1420. ELSE
  1421. Error(parameter.position, "integer or range expected");
  1422. RETURN FALSE
  1423. END;
  1424. parameter := parameter.nextParameter
  1425. END;
  1426. END;
  1427. (*
  1428. - for read operators: check type of last parameter
  1429. - for write operators: check return type
  1430. *)
  1431. IF isReadOperator THEN
  1432. dataType := procedureType.returnType (* the return type *)
  1433. ELSE
  1434. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1435. END;
  1436. elementType := arrayStructure.ElementType();
  1437. hasTypeError := FALSE;
  1438. IF isGeneralOperator THEN
  1439. (* ARRAY [?] OF <Element> *)
  1440. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1441. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1442. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1443. hasTypeError := TRUE
  1444. END
  1445. ELSE
  1446. hasTypeError := TRUE
  1447. END
  1448. ELSE
  1449. IF rangeCount = 0 THEN
  1450. (* <Element> *)
  1451. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1452. ELSE
  1453. (* ARRAY [*, *, ..., *] OF <Element> *)
  1454. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1455. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1456. IF mathArrayType.IsFullyDynamic() THEN
  1457. IF mathArrayType.Dimensionality() = rangeCount THEN
  1458. otherElementType := mathArrayType.ElementType();
  1459. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1460. ELSE
  1461. hasTypeError := TRUE
  1462. END
  1463. ELSE
  1464. hasTypeError := TRUE
  1465. END
  1466. ELSE
  1467. hasTypeError := TRUE
  1468. END
  1469. END
  1470. END;
  1471. IF hasTypeError THEN
  1472. IF isReadOperator THEN
  1473. Error(operator.position, "return type does not match")
  1474. ELSE
  1475. Error(parameter.position, "type of last parameter does not match")
  1476. END;
  1477. RETURN FALSE
  1478. END;
  1479. (* export symbol automatically *)
  1480. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1481. RETURN TRUE
  1482. END CheckIndexOperator;
  1483. (** resolve all pending types (late resolving).
  1484. - type fixes are resolved at the end of the declaration phase
  1485. - type fixes may imply new type fixes that are also entered at the end of the list
  1486. **)
  1487. PROCEDURE FixTypes;
  1488. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1489. BEGIN
  1490. prevScope := currentScope;
  1491. p := typeFixes.Get(currentScope);
  1492. WHILE p # NIL DO
  1493. ASSERT(currentScope # NIL);
  1494. ASSERT(p IS SyntaxTree.Type);
  1495. IF p IS SyntaxTree.PointerType THEN
  1496. FixPointerType(p(SyntaxTree.PointerType))
  1497. ELSIF p IS SyntaxTree.ProcedureType THEN
  1498. FixProcedureType(p(SyntaxTree.ProcedureType))
  1499. ELSE
  1500. HALT(100);
  1501. END;
  1502. p := typeFixes.Get(currentScope);
  1503. END;
  1504. currentScope :=prevScope;
  1505. END FixTypes;
  1506. (**
  1507. resolve type x
  1508. - if x is nil then return nil
  1509. - if x cannot be resolved then the result is invalidType else the result is x
  1510. - the resolved type is entered into x.resolved
  1511. **)
  1512. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1513. VAR prev,resolved: SyntaxTree.Type;
  1514. BEGIN
  1515. prev := resolvedType;
  1516. resolvedType := SyntaxTree.invalidType;
  1517. IF x = NIL THEN resolvedType := NIL
  1518. ELSE VType(x); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1519. END;
  1520. resolved := resolvedType;
  1521. resolvedType := prev;
  1522. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1523. RETURN resolved
  1524. END ResolveType;
  1525. (*** compatibility rules ***)
  1526. (**
  1527. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1528. **)
  1529. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1530. VAR result: SyntaxTree.Type;
  1531. BEGIN
  1532. result := SyntaxTree.invalidType;
  1533. IF type = NIL THEN Error(position, "expression of type NIL");
  1534. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1535. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1536. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1537. ELSE result := type.resolved
  1538. END;
  1539. RETURN result
  1540. END RegularType;
  1541. (** returns signature compatibility of procedure types this and to
  1542. - if not compatible then error is reported
  1543. - compatibility means type equality
  1544. **)
  1545. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1546. VAR result: BOOLEAN;
  1547. BEGIN
  1548. result := SameType(to,this);
  1549. IF ~result THEN
  1550. Error(position, "signature incompatible");
  1551. IF VerboseErrorMessage THEN
  1552. Printout.Info("this",this);
  1553. Printout.Info("to",to);
  1554. END;
  1555. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1556. Error(position, "signature incompatible: realtime flag must be inherited");
  1557. END;
  1558. RETURN result
  1559. END SignatureCompatible;
  1560. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1561. - for var parameters compatibility means same type except for
  1562. - formal is of open array of system byte
  1563. - formal is of record type
  1564. - formal is of open array type
  1565. - formal is of open math array type
  1566. - for value parameters compatibllity means assignment compatibility except for
  1567. - formal is of open array type
  1568. if compatible the return true else report error and return false
  1569. **)
  1570. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1571. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1572. BEGIN
  1573. formalType := RegularType(formal.position,formal.type);
  1574. actualType := RegularType(actual.position,actual.type);
  1575. error := FALSE;
  1576. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1577. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1578. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1579. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1580. END;
  1581. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1582. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1583. ELSIF ~IsVariable(actual) THEN
  1584. result := FALSE; error := TRUE;
  1585. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1586. Error(actual.position,"not a variable: no operator for writing");
  1587. ELSE
  1588. Error(actual.position,"is not a variable");
  1589. END;
  1590. IF VerboseErrorMessage THEN
  1591. Printout.Info("actual",actual);
  1592. Printout.Info("formal",formal);
  1593. END;
  1594. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1595. result := CompatibleTo(system,actualType,formalType);
  1596. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1597. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1598. ELSIF IsUnsafePointer(formalType) & IsUnsafePointer(actualType) THEN
  1599. result := TRUE;
  1600. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1601. IF IsArrayStructuredObjectType(actualType) THEN
  1602. actualType := MathArrayStructureOfType(actualType)
  1603. END;
  1604. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1605. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1606. Error(actual.position,"incompatible non-static actual type");
  1607. END;
  1608. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1609. Error(actual.position,"incompatible tensor (use a range expression)");
  1610. END;
  1611. ELSE
  1612. result := SameType(actualType,formalType)
  1613. END
  1614. ELSE
  1615. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1616. actualType := system.characterType;
  1617. END;
  1618. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1619. result := TRUE; (* special rule for WINAPI parameters *)
  1620. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1621. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1622. ELSE
  1623. result := CompatibleTo(system,actualType,formalType);
  1624. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1625. Error(actual.position,"incompatible non-static actual type");
  1626. END;
  1627. END;
  1628. END;
  1629. IF ~result & ~error THEN
  1630. Error(actual.position,"incompatible parameter");
  1631. IF VerboseErrorMessage THEN
  1632. Printout.Info("actual",actual);
  1633. Printout.Info("formal",formal);
  1634. END;
  1635. END;
  1636. RETURN result
  1637. END ParameterCompatible;
  1638. (** check compatibility for expressions of the form left := right
  1639. - if compatible then return true else error report and return false
  1640. - check if left is variable
  1641. - check compatiblity
  1642. **)
  1643. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1644. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1645. BEGIN
  1646. result := FALSE;
  1647. leftType := RegularType(left.position,left.type);
  1648. rightType := RegularType(right.position,right.type);
  1649. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1650. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1651. END;
  1652. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1653. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1654. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1655. (* error already handled *)
  1656. result := TRUE;
  1657. ELSIF ~IsVariable(left) THEN
  1658. Error(left.position,"is not a variable");
  1659. IF VerboseErrorMessage THEN
  1660. Printout.Info("left",left);
  1661. Printout.Info("right",right);
  1662. END;
  1663. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1664. result := TRUE;
  1665. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1666. result := TRUE
  1667. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1668. Error(left.position,"incompatible assignment");
  1669. IF VerboseErrorMessage THEN
  1670. Printout.Info("left",left);
  1671. Printout.Info("right",right);
  1672. END;
  1673. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1674. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1675. Error(right.position,"forbidden assignment of a nested procedure");
  1676. ELSE
  1677. result := TRUE
  1678. END;
  1679. RETURN result
  1680. END AssignmentCompatible;
  1681. (*** values ***)
  1682. (** check and resolve integer value **)
  1683. PROCEDURE VisitIntegerValue*(value: SyntaxTree.IntegerValue);
  1684. VAR integer: Basic.Integer;
  1685. BEGIN
  1686. integer := value(SyntaxTree.IntegerValue).value;
  1687. value.SetType(Global.GetIntegerType(system,integer));
  1688. resolvedExpression := value
  1689. END VisitIntegerValue;
  1690. (** check and resolve real value **)
  1691. PROCEDURE VisitRealValue*(value: SyntaxTree.RealValue);
  1692. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1693. BEGIN
  1694. subtype := value(SyntaxTree.RealValue).subtype;
  1695. IF subtype = Scanner.Real THEN
  1696. type := system.realType
  1697. ELSIF subtype = Scanner.Longreal THEN
  1698. type := system.longrealType
  1699. ELSE
  1700. HALT(100)
  1701. END;
  1702. value.SetType(type);
  1703. resolvedExpression := value
  1704. END VisitRealValue;
  1705. (** check and resolve complex value **)
  1706. PROCEDURE VisitComplexValue*(value: SyntaxTree.ComplexValue);
  1707. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1708. BEGIN
  1709. subtype := value(SyntaxTree.ComplexValue).subtype;
  1710. IF subtype = Scanner.Real THEN
  1711. type := system.complexType
  1712. ELSIF subtype = Scanner.Longreal THEN
  1713. type := system.longcomplexType
  1714. ELSE
  1715. HALT(100)
  1716. END;
  1717. value.SetType(type);
  1718. resolvedExpression := value
  1719. END VisitComplexValue;
  1720. (** check and resolve set value **)
  1721. PROCEDURE VisitSetValue*(value: SyntaxTree.SetValue);
  1722. BEGIN
  1723. value.SetType(system.setType);
  1724. resolvedExpression := value
  1725. END VisitSetValue;
  1726. (** check and resolve set value **)
  1727. PROCEDURE VisitMathArrayValue*(value: SyntaxTree.MathArrayValue);
  1728. BEGIN
  1729. value.SetType(SyntaxTree.invalidType);
  1730. resolvedExpression := value
  1731. END VisitMathArrayValue;
  1732. (** check and resolve boolean value **)
  1733. PROCEDURE VisitBooleanValue*(value: SyntaxTree.BooleanValue);
  1734. BEGIN
  1735. value.SetType(system.booleanType);
  1736. resolvedExpression := value
  1737. END VisitBooleanValue;
  1738. (** check and resolve string value **)
  1739. PROCEDURE VisitStringValue*(value: SyntaxTree.StringValue);
  1740. BEGIN
  1741. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1742. resolvedExpression := value
  1743. END VisitStringValue;
  1744. (** check and resolve character value **)
  1745. PROCEDURE VisitCharacterValue*(value: SyntaxTree.CharacterValue);
  1746. BEGIN
  1747. value.SetType(system.characterType);
  1748. resolvedExpression := value
  1749. END VisitCharacterValue;
  1750. (** check and resolve nil value **)
  1751. PROCEDURE VisitNilValue*(value: SyntaxTree.NilValue);
  1752. BEGIN
  1753. value.SetType(system.nilType);
  1754. resolvedExpression := value
  1755. END VisitNilValue;
  1756. (** check and resolve enumerator value **)
  1757. PROCEDURE VisitEnumerationValue*(value: SyntaxTree.EnumerationValue);
  1758. BEGIN
  1759. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1760. ASSERT(value.type # NIL);
  1761. resolvedExpression := value
  1762. END VisitEnumerationValue;
  1763. (*** expressions ***)
  1764. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1765. - check all elements on integer type
  1766. - if element range is constant, then check lower and upper bound
  1767. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1768. if an error occurs then report error and return invalidExpression
  1769. **)
  1770. PROCEDURE VisitSet*(set: SyntaxTree.Set);
  1771. VAR
  1772. i: LONGINT;
  1773. element: SyntaxTree.Expression;
  1774. constant: BOOLEAN;
  1775. elements: SyntaxTree.ExpressionList;
  1776. s: Basic.Set;
  1777. result: SyntaxTree.Expression;
  1778. value: SyntaxTree.Value;
  1779. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1780. VAR
  1781. left, right: SyntaxTree.Expression;
  1782. elementResult: SyntaxTree.Expression;
  1783. leftInteger, rightInteger: Basic.Integer;
  1784. BEGIN
  1785. (* set context of range *)
  1786. IF element IS SyntaxTree.RangeExpression THEN
  1787. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1788. END;
  1789. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1790. IF elementResult = SyntaxTree.invalidExpression THEN
  1791. (* error already reported *)
  1792. constant := FALSE
  1793. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1794. (* the element is a range expression *)
  1795. (* extract left and right hand side of range *)
  1796. left := elementResult(SyntaxTree.RangeExpression).first;
  1797. right := elementResult(SyntaxTree.RangeExpression).last;
  1798. (* guaranteed by VisitRangeExpression: *)
  1799. ASSERT((left # NIL) & (right # NIL));
  1800. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1801. ELSE
  1802. (* the element is not a range expression *)
  1803. (* check type and add conversion if needed *)
  1804. IF IsIntegerType(elementResult.type.resolved) THEN
  1805. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1806. ELSE
  1807. Error(elementResult.position, "non integer element in set");
  1808. elementResult := SyntaxTree.invalidExpression;
  1809. constant := FALSE
  1810. END;
  1811. left := elementResult;
  1812. right := elementResult
  1813. END;
  1814. IF elementResult # SyntaxTree.invalidExpression THEN
  1815. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1816. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1817. Error(left.position,"not allowed set integer value");
  1818. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1819. Error(right.position,"not allowed set integer value");
  1820. END
  1821. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1822. Error(right.position,"not allowed set integer value");
  1823. ELSE
  1824. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1825. s := {};
  1826. ELSE
  1827. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1828. IF leftInteger < 0 THEN leftInteger := 0 END;
  1829. (*!!!!!!!!! this is a hack !!!!!!! *)
  1830. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1831. extends the range x..31 to x..63 !!!!!! *)
  1832. s := s + {leftInteger..rightInteger};
  1833. END;
  1834. END;
  1835. ELSE
  1836. constant := FALSE;
  1837. END
  1838. END;
  1839. RETURN elementResult
  1840. END CheckElement;
  1841. BEGIN
  1842. result := set; constant := TRUE; s := {}; elements := set.elements;
  1843. IF elements # NIL THEN
  1844. FOR i := 0 TO elements.Length()-1 DO
  1845. element := elements.GetExpression(i);
  1846. element := CheckElement(element);
  1847. IF element = SyntaxTree.invalidExpression THEN
  1848. result := SyntaxTree.invalidExpression
  1849. END;
  1850. elements.SetExpression(i,element);
  1851. END;
  1852. END;
  1853. IF constant THEN
  1854. value := Global.NewSetValue(system,set.position,s);
  1855. result.SetResolved(value);
  1856. result.SetType(value.type);
  1857. ELSE
  1858. result.SetType(system.setType);
  1859. END;
  1860. (* optimization possible
  1861. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1862. left this to the programmer...
  1863. *)
  1864. resolvedExpression := result;
  1865. END VisitSet;
  1866. (*
  1867. old variant: quite generic but needs better conversion handling, do this?
  1868. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1869. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1870. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1871. BEGIN
  1872. type := NIL;
  1873. numberElements := x.elements.Length();
  1874. FOR i := 0 TO numberElements-1 DO
  1875. expression := x.elements.GetExpression(i);
  1876. position := expression.position;
  1877. expression := ResolveExpression(x.elements.GetExpression(i));
  1878. x.elements.SetExpression(i,de);
  1879. IF type = NIL THEN
  1880. type := expression.type;
  1881. ELSIF CompatibleTo(system,expression.type,type) THEN
  1882. (* ok *)
  1883. ELSIF CompatibleTo(system,type,expression.type) THEN
  1884. type := expression.type
  1885. ELSE
  1886. Error(expression.position, "incompatible element types");
  1887. type := SyntaxTree.invalidType;
  1888. END;
  1889. END;
  1890. isValue := TRUE;
  1891. FOR i := 0 TO numberElements-1 DO
  1892. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1893. x.elements.SetExpression(i,expression);
  1894. isValue := isValue & (expression.resolved # NIL);
  1895. END;
  1896. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1897. arrayType.SetArrayBase(type);
  1898. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1899. IF isValue THEN
  1900. value := SyntaxTree.NewMathArrayValue(position);
  1901. value.SetElements(x.elements);
  1902. x.SetResolved(value);
  1903. END;
  1904. x.SetType(arrayType);
  1905. resolvedExpression := x;
  1906. END VisitMathArrayExpression;
  1907. *)
  1908. PROCEDURE VisitMathArrayExpression*(x: SyntaxTree.MathArrayExpression);
  1909. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1910. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1911. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1912. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1913. BEGIN
  1914. numberElements := x.elements.Length();
  1915. FOR i := 0 TO numberElements-1 DO
  1916. expression := x.elements.GetExpression(i);
  1917. IF expression IS SyntaxTree.MathArrayExpression THEN
  1918. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1919. ELSE
  1920. position := expression.position;
  1921. expression := ResolveExpression(x.elements.GetExpression(i));
  1922. x.elements.SetExpression(i,expression);
  1923. IF type = NIL THEN
  1924. type := expression.type;
  1925. ELSIF CompatibleTo(system,expression.type,type) THEN
  1926. (* ok *)
  1927. ELSIF CompatibleTo(system,type,expression.type) THEN
  1928. type := expression.type
  1929. ELSE
  1930. Error(expression.position, "incompatible element types");
  1931. type := SyntaxTree.invalidType;
  1932. END;
  1933. END;
  1934. END;
  1935. END RecursivelyFindType;
  1936. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1937. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1938. BEGIN
  1939. numberElements := x.elements.Length();
  1940. FOR i := 0 TO numberElements-1 DO
  1941. expression := x.elements.GetExpression(i);
  1942. IF expression IS SyntaxTree.MathArrayExpression THEN
  1943. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1944. ELSE
  1945. position := expression.position;
  1946. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1947. x.elements.SetExpression(i,expression);
  1948. isValue := isValue & (expression.resolved # NIL);
  1949. END;
  1950. END;
  1951. END RecursivelySetExpression;
  1952. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1953. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1954. arrayType: SyntaxTree.MathArrayType;
  1955. BEGIN
  1956. numberElements := x.elements.Length();
  1957. baseType := NIL;
  1958. gsize := 0;
  1959. FOR i := 0 TO numberElements-1 DO
  1960. expression := x.elements.GetExpression(i);
  1961. IF expression IS SyntaxTree.MathArrayExpression THEN
  1962. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1963. IF i=0 THEN
  1964. gsize := size;
  1965. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1966. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1967. ELSE expression.SetType(baseType)
  1968. END;
  1969. ELSIF baseType = NIL THEN baseType := type;
  1970. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1971. END;
  1972. END;
  1973. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1974. arrayType.SetArrayBase(baseType);
  1975. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1976. RETURN ResolveType(arrayType);
  1977. END RecursivelySetType;
  1978. BEGIN
  1979. type := NIL;
  1980. RecursivelyFindType(x);
  1981. isValue := TRUE;
  1982. RecursivelySetExpression(x);
  1983. arrayType := RecursivelySetType(x);
  1984. x.SetType(arrayType);
  1985. IF isValue THEN
  1986. value := SyntaxTree.NewMathArrayValue(x.position);
  1987. value.SetArray(x);
  1988. x.SetResolved(value);
  1989. value.SetType(arrayType);
  1990. END;
  1991. x.SetType(arrayType);
  1992. resolvedExpression := x;
  1993. END VisitMathArrayExpression;
  1994. (** check and resolve unary expression **)
  1995. PROCEDURE VisitUnaryExpression*(unaryExpression: SyntaxTree.UnaryExpression);
  1996. VAR
  1997. left: SyntaxTree.Expression;
  1998. int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; operator: LONGINT;
  1999. bool: BOOLEAN;
  2000. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  2001. value: SyntaxTree.Value;
  2002. BEGIN
  2003. type := SyntaxTree.invalidType;
  2004. left := ResolveExpression(unaryExpression.left);
  2005. unaryExpression.SetLeft(left);
  2006. operator := unaryExpression.operator;
  2007. result := unaryExpression;
  2008. IF ~system.operatorDefined[operator] THEN
  2009. Error(left.position,"Operator Not Defined");
  2010. RETURN
  2011. ELSIF left.type = NIL THEN
  2012. Error(left.position,"Invalid Nil Argument in Unary Expression");
  2013. resolvedExpression := SyntaxTree.invalidExpression;
  2014. RETURN
  2015. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2016. RETURN
  2017. END;
  2018. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2019. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2020. END;
  2021. IF operatorCall # NIL THEN
  2022. result := operatorCall;
  2023. type := operatorCall.type;
  2024. (* admissible operators
  2025. Minus number, set
  2026. Not boolean
  2027. *)
  2028. ELSE
  2029. CASE unaryExpression.operator OF
  2030. |Scanner.Minus:
  2031. IF IsIntegerType(left.type.resolved) THEN
  2032. IF left.resolved # NIL THEN
  2033. int := -left.resolved(SyntaxTree.IntegerValue).value;
  2034. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2035. result.SetResolved(value);
  2036. type := Global.GetIntegerType(system,int);
  2037. value.SetType(type);
  2038. ELSE
  2039. type := left.type
  2040. END
  2041. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2042. IF IsRealValue(left,real) THEN
  2043. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2044. result.SetResolved(value);
  2045. type := left.type;
  2046. value.SetType(type);
  2047. ELSE
  2048. type := left.type;
  2049. END;
  2050. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2051. IF IsSetValue(left,set) THEN
  2052. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2053. result.SetResolved(value);
  2054. type := Global.GetSetType(system,-set);
  2055. value.SetType(type);
  2056. ELSE
  2057. type := left.type;
  2058. END;
  2059. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2060. IF IsComplexValue(left, real, imaginary) THEN
  2061. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2062. result.SetResolved(value);
  2063. type := left.type;
  2064. value.SetType(type);
  2065. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2066. ELSE
  2067. type := left.type;
  2068. END
  2069. ELSE
  2070. Error(left.position,"unary operator not applicable");
  2071. END;
  2072. |Scanner.Not:
  2073. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2074. IF IsBooleanValue(left,bool) THEN
  2075. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2076. result.SetResolved(value);
  2077. type := system.booleanType;
  2078. value.SetType(type);
  2079. ELSE
  2080. type := system.booleanType;
  2081. END;
  2082. ELSE
  2083. Error(left.position,"unary operator not applicable");
  2084. END;
  2085. |Scanner.Plus:
  2086. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2087. result := left; type := left.type;
  2088. ELSE
  2089. Error(left.position,"unary operator not applicable");
  2090. END;
  2091. (* ADDRESS OF *)
  2092. |Scanner.Address:
  2093. IF HasAddress(left) THEN
  2094. type := system.addressType;
  2095. ELSE
  2096. type := SyntaxTree.invalidType;
  2097. Error(left.position,"has no address");
  2098. Printout.Info("par", left);
  2099. END;
  2100. (* SIZE OF *)
  2101. |Scanner.Size:
  2102. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2103. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2104. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2105. value := SyntaxTree.NewIntegerValue(left.position, int);
  2106. result.SetResolved(value);
  2107. type := Global.GetIntegerType(system,int);
  2108. value.SetType(type)
  2109. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2110. ELSE
  2111. (* for variables, system sizeof could represent the physically occupied size
  2112. determined via the type descriptor, implement that ? *)
  2113. Error(left.position,"is not a type symbol");
  2114. END
  2115. (* ALIAS OF *)
  2116. |Scanner.Alias:
  2117. type := left.type.resolved;
  2118. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2119. type := SyntaxTree.invalidType;
  2120. Error(left.position,"alias on non math array type");
  2121. END;
  2122. ELSE
  2123. Error(left.position,"unary operator not defined");
  2124. END;
  2125. END;
  2126. result.SetType(type);
  2127. resolvedExpression := result
  2128. END VisitUnaryExpression;
  2129. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2130. VAR
  2131. result: SyntaxTree.Expression;
  2132. array: SyntaxTree.MathArrayExpression;
  2133. value: SyntaxTree.MathArrayValue;
  2134. isValue: BOOLEAN;
  2135. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2136. BEGIN
  2137. type := type.resolved;
  2138. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2139. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2140. END;
  2141. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2142. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2143. END;
  2144. RETURN type
  2145. END BaseType;
  2146. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2147. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2148. BEGIN
  2149. numberElements := x.elements.Length();
  2150. FOR i := 0 TO numberElements-1 DO
  2151. expression := x.elements.GetExpression(i);
  2152. IF expression IS SyntaxTree.MathArrayExpression THEN
  2153. array := SyntaxTree.NewMathArrayExpression(position);
  2154. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2155. expression := array;
  2156. ELSE
  2157. position := expression.position;
  2158. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2159. isValue := isValue & (expression.resolved # NIL);
  2160. END;
  2161. to.elements.AddExpression(expression);
  2162. END;
  2163. END RecursivelyConvert;
  2164. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2165. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2166. arrayType: SyntaxTree.MathArrayType;
  2167. BEGIN
  2168. numberElements := x.elements.Length();
  2169. baseType := NIL;
  2170. gsize := 0;
  2171. FOR i := 0 TO numberElements-1 DO
  2172. expression := x.elements.GetExpression(i);
  2173. IF expression IS SyntaxTree.MathArrayExpression THEN
  2174. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2175. IF i=0 THEN
  2176. gsize := size;
  2177. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2178. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2179. ELSE expression.SetType(baseType)
  2180. END;
  2181. ELSIF baseType = NIL THEN baseType := type;
  2182. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2183. END;
  2184. END;
  2185. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2186. arrayType.SetArrayBase(baseType);
  2187. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2188. RETURN ResolveType(arrayType);
  2189. END RecursivelySetType;
  2190. BEGIN
  2191. result := SyntaxTree.invalidExpression;
  2192. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2193. result := expression (* do not convert *)
  2194. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2195. isValue := TRUE;
  2196. type := BaseType(type);
  2197. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2198. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2199. value := SyntaxTree.NewMathArrayValue(array.position);
  2200. value.SetArray(array);
  2201. value.SetType(RecursivelySetType(array));
  2202. result := value;
  2203. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2204. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2205. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2206. IF result = NIL THEN
  2207. result := SyntaxTree.invalidExpression;
  2208. Error(position, "incompatible conversion");
  2209. IF VerboseErrorMessage THEN
  2210. Printout.Info("expression",expression);
  2211. Printout.Info("type",type);
  2212. END;
  2213. END;
  2214. END;
  2215. RETURN result
  2216. END MathArrayConversion;
  2217. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2218. VAR result: SyntaxTree.Expression; int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; char: CHAR; string: Scanner.StringType;
  2219. BEGIN
  2220. result := expression; type := type.resolved;
  2221. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2222. (* skip, no conversion *)
  2223. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2224. int := expression(SyntaxTree.IntegerValue).value;
  2225. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2226. int := Global.ConvertSigned(int,system.SizeOf(type));
  2227. result := SyntaxTree.NewIntegerValue(position,int);
  2228. result.SetType(type);
  2229. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2230. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2231. result := SyntaxTree.NewIntegerValue(position,int);
  2232. result.SetType(type);
  2233. ELSIF (type IS SyntaxTree.FloatType) THEN
  2234. result := SyntaxTree.NewRealValue(expression.position,int);
  2235. result.SetType(type);
  2236. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2237. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2238. result.SetType(type);
  2239. ELSIF (type IS SyntaxTree.SetType) THEN
  2240. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,int));
  2241. result.SetType(type);
  2242. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2243. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2244. result.SetType(type);
  2245. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2246. result := SyntaxTree.NewEnumerationValue(expression.position,int);
  2247. result.SetType(type);
  2248. ELSIF (type IS SyntaxTree.PortType) THEN
  2249. result := ConvertValue(position, expression, system.integerType);
  2250. ELSE
  2251. Error(position, "integer value cannot be converted");
  2252. result := SyntaxTree.invalidExpression;
  2253. IF VerboseErrorMessage THEN
  2254. Printout.Info("expression",expression);
  2255. Printout.Info("type",type);
  2256. END;
  2257. END;
  2258. ELSIF IsRealValue(expression,real) THEN
  2259. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2260. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2261. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2262. result.SetType(type);
  2263. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2264. int := ENTIERH(real);
  2265. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2266. result.SetType(type);
  2267. ELSIF (type IS SyntaxTree.FloatType) THEN
  2268. result := SyntaxTree.NewRealValue(position,real);
  2269. result.SetType(type);
  2270. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2271. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2272. result.SetType(type);
  2273. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2274. ELSIF (type IS SyntaxTree.PortType) THEN
  2275. result := ConvertValue(position, expression, system.integerType);
  2276. ELSE
  2277. Error(position, "real value cannot be converted");
  2278. result := SyntaxTree.invalidExpression;
  2279. END
  2280. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2281. IF (type IS SyntaxTree.ComplexType) THEN
  2282. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2283. result.SetType(type);
  2284. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2285. ELSE
  2286. Error(position, "complex value cannot be converted");
  2287. result := SyntaxTree.invalidExpression;
  2288. END
  2289. ELSIF IsSetValue(expression,set) THEN
  2290. IF (type IS SyntaxTree.IntegerType) THEN
  2291. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,set));
  2292. result.SetType(type);
  2293. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(Basic.Integer,set)) *)
  2294. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2295. result.SetType(type);
  2296. ELSIF (type IS SyntaxTree.PortType) THEN
  2297. result := ConvertValue(position, expression, system.integerType);
  2298. ELSIF (type IS SyntaxTree.SetType) THEN
  2299. set := Global.ConvertSet(set,system.SizeOf(type));
  2300. result := SyntaxTree.NewSetValue(expression.position,set);
  2301. result.SetType(type);
  2302. ELSE
  2303. Error(position, "set value cannot be converted");
  2304. result := SyntaxTree.invalidExpression;
  2305. END;
  2306. ELSIF IsStringValue(expression,string) THEN
  2307. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2308. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2309. result.SetType(type);
  2310. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2311. ELSE
  2312. Error(position, "string value cannot be converted");
  2313. result := SyntaxTree.invalidExpression;
  2314. END;
  2315. ELSIF IsCharacterValue(expression,char) THEN
  2316. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2317. string[0] := char; string[1] := 0X;
  2318. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2319. result := SyntaxTree.NewStringValue(expression.position,string);
  2320. result.SetType(type);
  2321. ELSIF (type IS SyntaxTree.ByteType) THEN
  2322. (* do not simply set the new type as this could invalidate types of constants *)
  2323. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2324. result.SetType(type)
  2325. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2326. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,char));
  2327. result.SetType(type);
  2328. ELSIF (type IS SyntaxTree.SetType) THEN
  2329. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,Basic.Integer(ORD(char))));
  2330. result.SetType(type);
  2331. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2332. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2333. result.SetType(type);
  2334. ELSIF (type IS SyntaxTree.PortType) THEN
  2335. result := ConvertValue(position, expression, system.integerType);
  2336. ELSE
  2337. Error(position, "character value cannot be converted");
  2338. result := SyntaxTree.invalidExpression;
  2339. END;
  2340. ELSIF expression IS SyntaxTree.NilValue THEN
  2341. IF type IS SyntaxTree.AddressType THEN
  2342. result := SyntaxTree.NewIntegerValue(position,0);
  2343. result.SetType(type);
  2344. ELSE
  2345. result := expression;
  2346. END;
  2347. (* nothing to be done *)
  2348. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2349. result := MathArrayConversion(position, expression,type);
  2350. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2351. int := expression(SyntaxTree.EnumerationValue).value;
  2352. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2353. int := Global.ConvertSigned(int,system.SizeOf(type));
  2354. result := SyntaxTree.NewIntegerValue(position,int);
  2355. result.SetType(type);
  2356. ELSE
  2357. result := expression;
  2358. END;
  2359. (* nothing to be done *)
  2360. ELSE
  2361. Error(position, "expression cannot be converted");
  2362. IF VerboseErrorMessage THEN
  2363. Printout.Info("expression",expression);
  2364. Printout.Info("type",type);
  2365. END;
  2366. result := SyntaxTree.invalidExpression;
  2367. END;
  2368. RETURN result
  2369. END ConvertValue;
  2370. (**
  2371. return a conversion of an expression to a given type
  2372. - if expression is already of same type then return expression
  2373. - if incompatible conversion then report error and return invalidExpression
  2374. **)
  2375. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2376. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2377. BEGIN
  2378. type := type.resolved;
  2379. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2380. result := expression;
  2381. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2382. ELSIF expression = NIL THEN (* NIL expression *)
  2383. ELSIF expression.type = NIL THEN
  2384. Error(position, "expression of type NIL cannot be converted");
  2385. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2386. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2387. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2388. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2389. ELSIF expression.resolved # NIL THEN (* value *)
  2390. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2391. IF value IS SyntaxTree.Value THEN
  2392. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2393. result.SetResolved(value(SyntaxTree.Value));
  2394. result.SetType(value.type);
  2395. ELSE
  2396. result := value
  2397. END;
  2398. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2399. expressionList := SyntaxTree.NewExpressionList();
  2400. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2401. typeDeclaration.SetDeclaredType(type);
  2402. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2403. typeSymbol.SetType(typeDeclaration.type);
  2404. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2405. expressionList.AddExpression(expression);
  2406. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2407. result.SetType(type);
  2408. ELSIF IsArrayStructuredObjectType(type) THEN
  2409. (* no type can be converted to an array-structured object type *)
  2410. HALT(100)
  2411. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2412. IF inConversion>5 THEN
  2413. Error(expression.position,"recursive Conversion");
  2414. IF VerboseErrorMessage THEN
  2415. Printout.Info("expression",expression);
  2416. Printout.Info("type",type);
  2417. END;
  2418. END;
  2419. INC(inConversion);
  2420. IF inConversion < 10 THEN
  2421. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2422. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  2423. result := expression
  2424. ELSE
  2425. result := MathArrayConversion(position, expression,type);
  2426. END;
  2427. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2428. expression := ConvertToMathArray(expression);
  2429. type := MathArrayStructureOfType(type);
  2430. result := MathArrayConversion(position, expression, type)
  2431. ELSE
  2432. Error(expression.position,"cannot convert non array type to array type")
  2433. END;
  2434. END;
  2435. DEC(inConversion);
  2436. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2437. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  2438. result := expression; result.SetType(type);
  2439. ELSIF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2440. OR ~(type IS SyntaxTree.ArrayType) THEN
  2441. Error(expression.position,"cannot convert array type to non-array type")
  2442. END;
  2443. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2444. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2445. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2446. (*skip, no conversion*)
  2447. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2448. (* skip, no conversion *)
  2449. ELSE
  2450. ASSERT(~(type IS SyntaxTree.RangeType));
  2451. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2452. ASSERT(type # NIL);
  2453. END;
  2454. RETURN result
  2455. END NewConversion;
  2456. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2457. BEGIN
  2458. IF CompatibleTo(system,expression.type, type) THEN
  2459. RETURN NewConversion(position, expression, type, NIL);
  2460. ELSE
  2461. Error(expression.position, "incompatible expression");
  2462. RETURN SyntaxTree.invalidExpression
  2463. END;
  2464. END CompatibleConversion;
  2465. (**
  2466. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2467. **)
  2468. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2469. VAR leftType,rightType: SyntaxTree.Type;
  2470. BEGIN
  2471. IF left.type = NIL THEN Error(left.position,"no type")
  2472. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2473. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2474. (* error already handled *)
  2475. ELSE
  2476. leftType := left.type.resolved; rightType := right.type.resolved;
  2477. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2478. right := NewConversion(right.position, right, leftType, NIL);
  2479. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2480. left := NewConversion(left.position,left,rightType,NIL);
  2481. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2482. right := NewConversion(right.position, right, leftType, NIL);
  2483. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2484. left := NewConversion(left.position,left,rightType,NIL);
  2485. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2486. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2487. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2488. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2489. ELSIF
  2490. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2491. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2492. (* must be the case LONGREAL / COMPLEX ) *)
  2493. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2494. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2495. ELSE
  2496. Error(left.position,"incompatible operands");
  2497. END;
  2498. END;
  2499. END ConvertOperands;
  2500. (** find and return best operator matching to parameter list (nil, if none)
  2501. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2502. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2503. **)
  2504. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2505. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2506. identifier: SyntaxTree.Identifier;
  2507. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2508. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2509. BEGIN
  2510. operator := scope.firstOperator;
  2511. WHILE(operator # NIL) DO
  2512. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2513. procedureType := operator.type(SyntaxTree.ProcedureType);
  2514. distance := Distance(system, procedureType,actualParameters);
  2515. IF (distance < Infinity) THEN
  2516. IF returnType # NIL THEN
  2517. IF procedureType.returnType = NIL THEN
  2518. distance := Infinity
  2519. ELSE
  2520. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2521. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2522. END;
  2523. END;
  2524. END;
  2525. (*
  2526. IF distance < Infinity THEN
  2527. TRACE(distance, operator);
  2528. Printout.Info("potential operator",operator);
  2529. ELSE
  2530. Printout.Info("operator not possible",operator);
  2531. END;
  2532. *)
  2533. IF distance < bestDistance THEN
  2534. bestDistance := distance;
  2535. bestOperator := operator;
  2536. END;
  2537. END;
  2538. operator := operator.nextOperator;
  2539. END;
  2540. (*
  2541. Printout.Info("taken operator",bestOperator);
  2542. *)
  2543. END FindInScope;
  2544. BEGIN
  2545. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2546. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2547. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2548. import := currentScope.ownerModule.moduleScope.firstImport;
  2549. WHILE (bestDistance > 0) & (import # NIL) DO
  2550. IF import.module # NIL THEN
  2551. identifier := Global.GetIdentifier(operator,import.module.case);
  2552. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2553. END;
  2554. import := import.nextImport;
  2555. END;
  2556. RETURN bestOperator
  2557. END FindOperator;
  2558. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2559. BEGIN
  2560. currentScope := scope;
  2561. END SetCurrentScope;
  2562. (**
  2563. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2564. - handle LEN and DIM operator for array-structured object types
  2565. - find operator, if found then
  2566. - if in other module then add import designator
  2567. - create symbol designator for operator
  2568. - if error then return invalidExpression, if no operator then return NIL
  2569. **)
  2570. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2571. VAR
  2572. operator: SyntaxTree.Operator;
  2573. import: SyntaxTree.Import;
  2574. expression, result: SyntaxTree.Expression;
  2575. designator: SyntaxTree.Designator;
  2576. actualParameters, tempList: SyntaxTree.ExpressionList;
  2577. recordType: SyntaxTree.RecordType;
  2578. castReturnType : SyntaxTree.MathArrayType;
  2579. BEGIN
  2580. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2581. result := SyntaxTree.invalidExpression
  2582. ELSIF leftExpression = NIL THEN
  2583. result := NIL
  2584. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2585. (* LEN or DIM operator on array-structured object type *)
  2586. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2587. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2588. IF recordType.arrayAccessOperators.len = NIL THEN
  2589. Error(position, "call of undeclared LEN operator");
  2590. result := SyntaxTree.invalidExpression
  2591. ELSE
  2592. ASSERT(leftExpression IS SyntaxTree.Designator);
  2593. designator := leftExpression(SyntaxTree.Designator);
  2594. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2595. ASSERT(expression IS SyntaxTree.Designator);
  2596. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2597. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2598. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2599. result := designator
  2600. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2601. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2602. tempList := SyntaxTree.NewExpressionList();
  2603. tempList.AddExpression(rightExpression);
  2604. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2605. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2606. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2607. tempList := SyntaxTree.NewExpressionList();
  2608. tempList.AddExpression(designator);
  2609. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2610. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2611. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2612. END
  2613. END;
  2614. ELSE
  2615. IF ~complexNumbersImported THEN
  2616. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2617. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2618. THEN
  2619. (* operators on complex numbers *)
  2620. ImportModule(Global.ComplexNumbersName,position);
  2621. complexNumbersImported := TRUE;
  2622. END;
  2623. END;
  2624. (* import OCArrayBase if needed *)
  2625. IF ~arrayBaseImported THEN
  2626. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2627. IF op = Global.Dim THEN
  2628. (* not existing in OCArrayBase *)
  2629. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2630. (* not existing in OCArrayBase *)
  2631. ELSE
  2632. ImportModule(Global.ArrayBaseName,position);
  2633. arrayBaseImported := TRUE;
  2634. END
  2635. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2636. ImportModule(Global.ArrayBaseName,position);
  2637. arrayBaseImported := TRUE
  2638. END;
  2639. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2640. (* LEN(RANGE) *)
  2641. ImportModule(Global.ArrayBaseName,position);
  2642. arrayBaseImported := TRUE;
  2643. END;
  2644. END;
  2645. actualParameters := SyntaxTree.NewExpressionList();
  2646. actualParameters.AddExpression(leftExpression);
  2647. IF rightExpression # NIL THEN
  2648. actualParameters.AddExpression(rightExpression)
  2649. END;
  2650. operator := FindOperator(system,op,actualParameters,resultType);
  2651. IF operator # NIL THEN
  2652. designator := NIL;
  2653. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2654. import := currentScope.ownerModule.moduleScope.firstImport;
  2655. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2656. import := import.nextImport;
  2657. END;
  2658. expression := NewSymbolDesignator(position,NIL,import);
  2659. designator := expression(SyntaxTree.Designator);
  2660. END;
  2661. expression := NewSymbolDesignator(position,designator,operator);
  2662. designator := expression(SyntaxTree.Designator);
  2663. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2664. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2665. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2666. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2667. result.SetType(castReturnType);
  2668. END;
  2669. ELSE
  2670. result := NIL;
  2671. END;
  2672. END;
  2673. RETURN result
  2674. END NewOperatorCall;
  2675. (** check and resolve binary expression **)
  2676. (*! clean up *)
  2677. PROCEDURE VisitBinaryExpression*(binaryExpression: SyntaxTree.BinaryExpression);
  2678. VAR left,right,result: SyntaxTree.Expression;
  2679. leftType, rightType: SyntaxTree.Type;
  2680. il,ir: Basic.Integer; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: Basic.Integer;bl,br: BOOLEAN; sl,sr: Basic.Set; strl,strr: Scanner.StringType;
  2681. cl,cr: CHAR;
  2682. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2683. type: SyntaxTree.Type;
  2684. value: SyntaxTree.Value;
  2685. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: Basic.Integer;
  2686. integerConstantFolding: BOOLEAN;
  2687. list: SyntaxTree.ExpressionList;
  2688. PROCEDURE NewBool(v: BOOLEAN);
  2689. BEGIN
  2690. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2691. value.SetType(system.booleanType);
  2692. result.SetResolved(value);
  2693. type := system.booleanType
  2694. END NewBool;
  2695. PROCEDURE NewSet(v: Basic.Set);
  2696. BEGIN
  2697. value := Global.NewSetValue(system,binaryExpression.position,v);
  2698. result.SetResolved(value);
  2699. type := value.type;
  2700. END NewSet;
  2701. PROCEDURE NewInteger(v: Basic.Integer; t: SyntaxTree.Type);
  2702. BEGIN
  2703. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2704. (* type cast to "larger" type only if the value is still in the range *)
  2705. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2706. value.SetType(t);
  2707. END;
  2708. result.SetResolved(value);
  2709. type := value.type;
  2710. END NewInteger;
  2711. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2712. BEGIN
  2713. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2714. value.SetType(t);
  2715. result.SetResolved(value);
  2716. type := t;
  2717. END NewReal;
  2718. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2719. BEGIN
  2720. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2721. value.SetType(t);
  2722. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2723. result.SetResolved(value);
  2724. type := t;
  2725. END NewComplex;
  2726. BEGIN
  2727. type := SyntaxTree.invalidType;
  2728. left := ResolveExpression(binaryExpression.left);
  2729. right := ResolveExpression(binaryExpression.right);
  2730. binaryExpression.SetLeft(left);
  2731. binaryExpression.SetRight(right);
  2732. result := binaryExpression;
  2733. operator := binaryExpression.operator;
  2734. IF ~system.operatorDefined[operator] THEN
  2735. Error(left.position,"Operator Not Defined");
  2736. result := SyntaxTree.invalidExpression;
  2737. RETURN
  2738. END;
  2739. IF left.type = NIL THEN
  2740. Error(left.position,"Expression has no result type");
  2741. result := SyntaxTree.invalidExpression;
  2742. RETURN;
  2743. END;
  2744. IF right.type = NIL THEN
  2745. Error(right.position,"Expression has no result type");
  2746. result := SyntaxTree.invalidExpression;
  2747. RETURN;
  2748. END;
  2749. leftType := left.type.resolved; rightType := right.type.resolved;
  2750. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2751. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2752. END;
  2753. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2754. list := SyntaxTree.NewExpressionList();
  2755. list.AddExpression(right);
  2756. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2757. END;
  2758. IF operatorCall # NIL THEN
  2759. result := operatorCall;
  2760. type := operatorCall.type;
  2761. (* admissible operators:
  2762. Times, Plus, Minus numeric numeric numeric
  2763. set set set
  2764. Slash numeric numeric real /complex
  2765. set set set
  2766. Div , Mod integer integer integer
  2767. And, Or bool bool bool
  2768. Equal, Unequal basic basic bool
  2769. pointer pointer bool
  2770. object object bool
  2771. record record bool
  2772. string string bool
  2773. enumerator enumerator bool
  2774. Less, LessEqual,
  2775. Greater, GreaterEqual integer/real integer/real bool
  2776. enumerator enumerator bool
  2777. In integer set bool
  2778. Is pointer type bool
  2779. object type bool
  2780. record type bool
  2781. Upto: special abbreviation for a..b
  2782. *)
  2783. ELSIF (left.type = NIL) THEN
  2784. Error(left.position,"type (left operand) = NIL in binary expression");
  2785. D.Str("nil type in "); D.Type(left); D.Ln;
  2786. result := SyntaxTree.invalidExpression;
  2787. ELSIF (right.type = NIL) THEN
  2788. Error(right.position,"type (right operand) = NIL in binary expression");
  2789. result := SyntaxTree.invalidExpression;
  2790. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2791. result := SyntaxTree.invalidExpression;
  2792. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2793. HALT(100);
  2794. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2795. type := system.booleanType;
  2796. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2797. Error(right.position,"is not a type ");
  2798. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2799. Error(binaryExpression.position,"is not a type extension of ");
  2800. IF VerboseErrorMessage THEN
  2801. Printout.Info("left",left);
  2802. Printout.Info("right",right);
  2803. END;
  2804. ELSIF IsUnsafePointer(left.type) THEN
  2805. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2806. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2807. NewBool(TRUE)
  2808. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2809. NewBool(TRUE);
  2810. ELSIF IsUnextensibleRecord(left) THEN
  2811. NewBool(FALSE)
  2812. END
  2813. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2814. Error(right.position,"must not be a type");
  2815. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2816. Error(left.position,"must not be a type");
  2817. ELSIF operator = Scanner.In THEN (* left IN right *)
  2818. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2819. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2820. NewBool(il IN sr);
  2821. ELSE
  2822. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2823. left := NewConversion(left.position, left, system.longintType,NIL);
  2824. binaryExpression.SetLeft(left)
  2825. END;
  2826. type := system.booleanType;
  2827. END
  2828. ELSE
  2829. Error(binaryExpression.position, "incompatible operands");
  2830. END
  2831. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2832. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2833. Error(binaryExpression.position,"incompatible operands");
  2834. END;
  2835. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2836. ELSE Error(binaryExpression.position,"operator not defined 1")
  2837. END
  2838. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2839. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2840. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2841. Error(binaryExpression.position,"incompatible operands");
  2842. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2843. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2844. left := NewConversion(left.position, left, system.addressType, NIL);
  2845. right := NewConversion(right.position, right, system.addressType, NIL);
  2846. binaryExpression.SetLeft(left);
  2847. binaryExpression.SetRight(right);
  2848. type := system.addressType;
  2849. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2850. ConvertOperands(left, right);
  2851. binaryExpression.SetLeft(left);
  2852. binaryExpression.SetRight(right);
  2853. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2854. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2855. END;
  2856. type := system.booleanType;
  2857. ELSE
  2858. Error(binaryExpression.position,"operator not defined 3");
  2859. END
  2860. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2861. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2862. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2863. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2864. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2865. CASE operator OF
  2866. |Scanner.Equal: NewBool(strl^=strr^);
  2867. |Scanner.Unequal:NewBool(strl^#strr^);
  2868. |Scanner.Less: NewBool(strl^<strr^);
  2869. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2870. |Scanner.Greater: NewBool(strl^>strr^);
  2871. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2872. ELSE
  2873. Error(binaryExpression.position,"operator not defined 4");
  2874. END;
  2875. END;
  2876. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2877. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2878. type := system.booleanType
  2879. ELSE
  2880. Error(binaryExpression.position,"operator not defined 5");
  2881. END;
  2882. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2883. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2884. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2885. type := system.booleanType;
  2886. ELSE
  2887. Error(binaryExpression.position,"operator not defined 6");
  2888. END
  2889. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2890. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2891. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2892. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2893. type := system.booleanType
  2894. ELSE
  2895. Error(binaryExpression.position,"operator not defined for enumerators");
  2896. END;
  2897. ELSE
  2898. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2899. END;
  2900. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2901. type := system.booleanType;
  2902. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2903. type := system.booleanType;
  2904. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2905. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2906. THEN
  2907. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2908. IF (leftType # rightType) THEN
  2909. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2910. ConvertOperands(left,right); (* operands must be of the same type here *)
  2911. END;
  2912. binaryExpression.SetLeft(left);
  2913. binaryExpression.SetRight(right);
  2914. leftType := left.type.resolved;
  2915. rightType := right.type.resolved;
  2916. END;
  2917. type := leftType;
  2918. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2919. Error(binaryExpression.position,"conversion failed ?");
  2920. IF VerboseErrorMessage THEN
  2921. Printout.Info("left",left);
  2922. Printout.Info("right",right);
  2923. END;
  2924. ELSIF IsIntegerType(leftType) THEN
  2925. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2926. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2927. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2928. Error(binaryExpression.position,"division by zero");
  2929. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2930. Error(binaryExpression.position,"integer division by negative number");
  2931. END;
  2932. END;
  2933. (* constant folding *)
  2934. (* bootstrap64
  2935. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2936. CASE operator OF
  2937. |Scanner.Plus: NewInteger(il+ir,left.type);
  2938. |Scanner.Minus: NewInteger(il-ir,left.type);
  2939. |Scanner.Times: NewInteger(il*ir,left.type);
  2940. |Scanner.Slash:
  2941. IF ir # 0 THEN
  2942. NewReal(il/ir, system.realType);
  2943. END;
  2944. |Scanner.Mod:
  2945. IF ir > 0 THEN
  2946. NewInteger(il MOD ir,left.type);
  2947. END;
  2948. |Scanner.Div:
  2949. IF ir > 0 THEN
  2950. NewInteger(il DIV ir,left.type);
  2951. END;
  2952. |Scanner.Equal: NewBool(il=ir);
  2953. |Scanner.Unequal:NewBool(il#ir);
  2954. |Scanner.Less: NewBool(il<ir);
  2955. |Scanner.LessEqual: NewBool(il<=ir);
  2956. |Scanner.Greater: NewBool(il>ir);
  2957. |Scanner.GreaterEqual: NewBool(il>=ir);
  2958. ELSE Error(binaryExpression.position,"operator not defined 7");
  2959. END;
  2960. ELS*)
  2961. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2962. hl := left.resolved(SyntaxTree.IntegerValue).value;
  2963. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2964. CASE operator OF
  2965. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2966. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2967. |Scanner.Times: NewInteger(hl*hr,left.type);
  2968. |Scanner.Slash:
  2969. IF hr = 0 THEN
  2970. Error(binaryExpression.position,"division by zero");
  2971. ELSE
  2972. IF type.sizeInBits = 64 THEN
  2973. NewReal(hl/hr,system.longrealType);
  2974. ELSE
  2975. NewReal(hl/hr,system.realType)
  2976. END
  2977. END;
  2978. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2979. |Scanner.Mod:
  2980. IF hr = 0 THEN
  2981. Error(binaryExpression.position,"division by zero");
  2982. ELSE
  2983. NewInteger(hl MOD hr, left.type);
  2984. (* bootstrap64
  2985. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2986. *)
  2987. END;
  2988. |Scanner.Div:
  2989. IF hr = 0 THEN
  2990. Error(binaryExpression.position,"division by zero");
  2991. ELSE
  2992. NewInteger(hl DIV hr, left.type);
  2993. (* bootstrap64
  2994. NewInteger(Machine.DivH(hl,hr),left.type);
  2995. *)
  2996. END;
  2997. (* *)
  2998. |Scanner.Equal: NewBool(hl=hr);
  2999. |Scanner.Unequal: NewBool(hl#hr);
  3000. |Scanner.Less: NewBool(hl<hr);
  3001. |Scanner.LessEqual: NewBool(hl<=hr);
  3002. |Scanner.Greater: NewBool(hl>hr);
  3003. |Scanner.GreaterEqual:NewBool(hl>=hr);
  3004. ELSE Error(binaryExpression.position,"operator not defined 8");
  3005. END;
  3006. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  3007. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  3008. type := left.type
  3009. ELSIF (operator = Scanner.Slash) THEN
  3010. left := NewConversion(left.position,left,system.realType,NIL);
  3011. right := NewConversion(right.position,right,system.realType,NIL);
  3012. binaryExpression.SetLeft(left);
  3013. binaryExpression.SetRight(right);
  3014. type := system.realType
  3015. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3016. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3017. type := system.booleanType
  3018. ELSE
  3019. Error(binaryExpression.position,"operator not defined 9");
  3020. END;
  3021. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  3022. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  3023. CASE operator OF
  3024. |Scanner.Plus: NewReal(rl+rr,leftType);
  3025. |Scanner.Minus: NewReal(rl-rr,leftType);
  3026. |Scanner.Times:NewReal(rl*rr,leftType);
  3027. |Scanner.Slash:
  3028. IF rr = 0 THEN
  3029. Error(binaryExpression.position,"division by zero");
  3030. ELSE
  3031. NewReal(rl/rr,leftType);
  3032. END
  3033. |Scanner.Equal: NewBool(rl=rr);
  3034. |Scanner.Unequal: NewBool(rl#rr);
  3035. |Scanner.Less: NewBool(rl<rr);
  3036. |Scanner.LessEqual: NewBool(rl<=rr);
  3037. |Scanner.Greater: NewBool(rl>rr);
  3038. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3039. ELSE Error(binaryExpression.position,"operator not defined 10");
  3040. END;
  3041. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3042. type := left.type
  3043. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3044. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3045. type := system.booleanType
  3046. ELSE
  3047. Error(binaryExpression.position,"operator not defined 11");
  3048. IF VerboseErrorMessage THEN
  3049. Printout.Info("left",left);
  3050. Printout.Info("right",right);
  3051. END;
  3052. END;
  3053. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3054. CASE operator OF
  3055. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3056. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3057. ELSE
  3058. Error(binaryExpression.position,"operator not defined");
  3059. IF VerboseErrorMessage THEN
  3060. Printout.Info("left", left);
  3061. Printout.Info("right", right)
  3062. END;
  3063. END;
  3064. IF ~error THEN
  3065. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3066. Error(binaryExpression.position,"division by zero")
  3067. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3068. (* do constant folding *)
  3069. CASE operator OF
  3070. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3071. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3072. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3073. |Scanner.Slash:
  3074. divisor := c * c + d * d;
  3075. ASSERT(divisor # 0);
  3076. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3077. |Scanner.Equal: NewBool((a = c) & (b = d))
  3078. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3079. END
  3080. END
  3081. END
  3082. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3083. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3084. CASE operator OF
  3085. |Scanner.And: NewBool(bl & br);
  3086. |Scanner.Or: NewBool(bl OR br);
  3087. |Scanner.Equal: NewBool(bl = br);
  3088. |Scanner.Unequal: NewBool(bl # br);
  3089. ELSE Error(binaryExpression.position,"operator not defined 12");
  3090. END;
  3091. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3092. type := system.booleanType
  3093. ELSE
  3094. Error(binaryExpression.position,"operator not defined 13");
  3095. END;
  3096. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3097. (* constant folding *)
  3098. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3099. IF operator = Scanner.Equal THEN
  3100. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3101. ELSIF operator = Scanner.Unequal THEN
  3102. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3103. END;
  3104. END;
  3105. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3106. type := system.booleanType;
  3107. ELSE
  3108. Error(binaryExpression.position, "operator not defined");
  3109. END;
  3110. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3111. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3112. CASE operator OF
  3113. |Scanner.Plus: NewSet(sl + sr);
  3114. |Scanner.Minus: NewSet(sl - sr);
  3115. |Scanner.Times: NewSet(sl * sr);
  3116. |Scanner.Slash: NewSet(sl / sr);
  3117. |Scanner.Equal: NewBool(sl=sr);
  3118. |Scanner.Unequal: NewBool(sl#sr);
  3119. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3120. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3121. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3122. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3123. ELSE Error(binaryExpression.position,"operator not defined 14");
  3124. END;
  3125. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3126. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3127. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3128. THEN
  3129. type := system.booleanType
  3130. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3131. type := left.type
  3132. ELSE
  3133. Error(binaryExpression.position,"operator not defined 15");
  3134. END;
  3135. ELSIF IsCharacterType(left.type) THEN
  3136. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3137. CASE operator OF
  3138. |Scanner.Equal: NewBool(cl=cr);
  3139. |Scanner.Unequal: NewBool(cl#cr);
  3140. |Scanner.Less: NewBool(cl<cr);
  3141. |Scanner.LessEqual: NewBool(cl<=cr);
  3142. |Scanner.Greater: NewBool(cl>cr);
  3143. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3144. ELSE Error(binaryExpression.position,"operator not defined 16");
  3145. END;
  3146. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3147. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3148. type := system.booleanType
  3149. ELSE
  3150. Error(binaryExpression.position,"operator not defined 17");
  3151. END;
  3152. ELSE
  3153. Error(binaryExpression.position,"operator not defined 18");
  3154. END;
  3155. ELSE
  3156. Error(binaryExpression.position,"operator not defined 19");
  3157. END;
  3158. IF type = SyntaxTree.invalidType THEN
  3159. result := SyntaxTree.invalidExpression
  3160. ELSE
  3161. result.SetType(type)
  3162. END;
  3163. resolvedExpression := result
  3164. END VisitBinaryExpression;
  3165. (** resolve a range expression of the from <<first .. last BY step>>
  3166. - depending on the context different things are checked:
  3167. ArrayIndex:
  3168. - components must be integers
  3169. - replace missing lower bound with 0
  3170. - replace missing upper bound with MAX(LONGINT)
  3171. - replace missing step size with 1
  3172. SetElement:
  3173. - components must be integers
  3174. - replace missing lower bound with 0
  3175. - replace missing upper bound with MAX(SET)
  3176. - must not have step size
  3177. CaseGuard:
  3178. - components must be constant
  3179. - components must be integers or characters
  3180. - must have lower and upper bound present
  3181. - components are made compatible
  3182. - must not have step size
  3183. - if error: return invalidExpression
  3184. **)
  3185. PROCEDURE VisitRangeExpression*(x: SyntaxTree.RangeExpression);
  3186. VAR
  3187. hasError: BOOLEAN;
  3188. first, last, step: SyntaxTree.Expression;
  3189. BEGIN
  3190. hasError := FALSE;
  3191. first := x.first;
  3192. last := x.last;
  3193. step := x.step;
  3194. (* check lower bound *)
  3195. IF x.context = SyntaxTree.CaseGuard THEN
  3196. IF first = NIL THEN
  3197. Error(x.position, "missing lower bound");
  3198. hasError := TRUE
  3199. ELSE
  3200. first := ResolveExpression(first);
  3201. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3202. Error(first.position, "lower bound not integer or character");
  3203. hasError := TRUE
  3204. ELSE
  3205. IF first IS SyntaxTree.StringValue THEN
  3206. (* add conversion from string to character *)
  3207. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3208. END
  3209. END;
  3210. (* check if expression is constant *)
  3211. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3212. (* error already reported *)
  3213. hasError := TRUE
  3214. END
  3215. END
  3216. ELSE (* ArrayIndex, SetElement *)
  3217. IF first = NIL THEN
  3218. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3219. END;
  3220. first := ResolveExpression(first);
  3221. IF IsIntegerType(first.type.resolved) THEN
  3222. first := NewConversion(first.position, first, system.lenType, NIL)
  3223. ELSE
  3224. Error(first.position, "lower bound not integer");
  3225. hasError := TRUE
  3226. END
  3227. END;
  3228. (* check upper bound *)
  3229. IF x.context = SyntaxTree.CaseGuard THEN
  3230. IF last = NIL THEN
  3231. Error(x.position, "missing upper bound");
  3232. hasError := TRUE
  3233. ELSE
  3234. last := ResolveExpression(last);
  3235. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3236. Error(last.position, "lower bound not integer or character");
  3237. hasError := TRUE
  3238. ELSE
  3239. IF last IS SyntaxTree.StringValue THEN
  3240. (* add conversion from string to character *)
  3241. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3242. END
  3243. END;
  3244. (* check if expression is constant *)
  3245. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3246. (* error already reported *)
  3247. hasError := TRUE
  3248. ELSE
  3249. (* try to make lower and upper bound compatible *)
  3250. ConvertOperands(first, last);
  3251. IF first.type.resolved # last.type.resolved THEN
  3252. Error(x.position, "lower and upper bounds incompatible");
  3253. hasError := TRUE
  3254. END
  3255. END
  3256. END
  3257. ELSE (* ArrayIndex, SetElement *)
  3258. IF last = NIL THEN
  3259. IF x.context = SyntaxTree.ArrayIndex THEN
  3260. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3261. ELSE
  3262. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3263. END
  3264. END;
  3265. last := ResolveExpression(last);
  3266. IF IsIntegerType(last.type.resolved) THEN
  3267. last := NewConversion(last.position, last, system.lenType, NIL)
  3268. ELSE
  3269. Error(last.position, "upper bound not integer");
  3270. hasError := TRUE
  3271. END
  3272. END;
  3273. (* check step size *)
  3274. IF x.context = SyntaxTree.ArrayIndex THEN
  3275. IF step = NIL THEN
  3276. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3277. END;
  3278. step := ResolveExpression(step);
  3279. IF IsIntegerType(step.type.resolved) THEN
  3280. step := NewConversion(step.position, step, system.lenType, NIL)
  3281. ELSE
  3282. Error(step.position, "step size not integer");
  3283. hasError := TRUE
  3284. END
  3285. ELSE (* SetElement, CaseGuard *)
  3286. IF step # NIL THEN
  3287. Error(last.position, "step size not allowed in this context");
  3288. hasError := TRUE
  3289. END
  3290. END;
  3291. IF hasError THEN
  3292. resolvedExpression := SyntaxTree.invalidExpression
  3293. ELSE
  3294. x.SetFirst(first);
  3295. x.SetLast(last);
  3296. x.SetStep(step);
  3297. x.SetType(system.rangeType);
  3298. resolvedExpression := x;
  3299. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3300. END
  3301. END VisitRangeExpression;
  3302. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3303. BEGIN
  3304. x.SetType(NIL);
  3305. resolvedExpression := x;
  3306. END VisitTensorRangeExpression;
  3307. (** resolve the expression d and return result as designator
  3308. - resolve expression
  3309. - if expression is a designator then return designator else error message and return invalidDesignator
  3310. **)
  3311. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3312. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3313. BEGIN
  3314. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3315. resolved := ResolveExpression(d);
  3316. IF resolved = SyntaxTree.invalidExpression THEN
  3317. (* error should already have been reported *)
  3318. result := SyntaxTree.invalidDesignator;
  3319. ELSIF resolved IS SyntaxTree.Designator THEN
  3320. result := resolved(SyntaxTree.Designator);
  3321. ELSE
  3322. Error(d.position,"is no designator ! ");
  3323. result := SyntaxTree.invalidDesignator;
  3324. END;
  3325. (* result.type might be nil. *)
  3326. RETURN result
  3327. END ResolveDesignator;
  3328. (**
  3329. symbol designator generated in this module
  3330. nothing to be resolved
  3331. **)
  3332. PROCEDURE VisitSymbolDesignator*(x: SyntaxTree.SymbolDesignator);
  3333. BEGIN
  3334. resolvedExpression := x;
  3335. END VisitSymbolDesignator;
  3336. (**
  3337. self designator generated in this module
  3338. nothing to be resolved
  3339. **)
  3340. PROCEDURE VisitSelfDesignator*(x: SyntaxTree.SelfDesignator);
  3341. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3342. BEGIN
  3343. (* check if in record scope *)
  3344. scope := currentScope;
  3345. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3346. scope := scope.outerScope;
  3347. END;
  3348. IF scope = NIL THEN (* in module scope *)
  3349. x.SetType(system.anyType);
  3350. ELSIF scope IS SyntaxTree.CellScope THEN
  3351. cell := scope(SyntaxTree.CellScope).ownerCell;
  3352. x.SetType(cell);
  3353. ELSE (* in record scope *)
  3354. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3355. IF (record # NIL) & (record.pointerType # NIL) THEN
  3356. type := ResolveType(record.pointerType);
  3357. x.SetType(type);
  3358. ELSE
  3359. x.SetType(record);
  3360. END;
  3361. END;
  3362. resolvedExpression := x;
  3363. END VisitSelfDesignator;
  3364. PROCEDURE VisitResultDesignator*(x: SyntaxTree.ResultDesignator);
  3365. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3366. BEGIN
  3367. scope := currentScope;
  3368. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3369. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3370. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3371. returnType := procedureType.returnType;
  3372. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3373. THEN
  3374. x.SetType(returnType);
  3375. ELSE
  3376. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3377. x.SetType(SyntaxTree.invalidType);
  3378. END;
  3379. ELSE
  3380. Error(x.position,"forbidden access to result designator");
  3381. x.SetType(SyntaxTree.invalidType);
  3382. END;
  3383. x.SetAssignable(TRUE);
  3384. resolvedExpression := x;
  3385. END VisitResultDesignator;
  3386. (**
  3387. return symbol designator as an expression
  3388. - if symbol is a constant then return the constant value expression
  3389. - else
  3390. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3391. - if symbol is a guarded variable then return a TypeGuardDesignator
  3392. - else return a symbol designator
  3393. **)
  3394. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3395. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3396. guardType: SyntaxTree.Type;
  3397. BEGIN
  3398. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3399. result := SyntaxTree.invalidExpression;
  3400. ASSERT(symbol # NIL);
  3401. (*
  3402. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3403. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3404. Error(position, "type not allowed here");
  3405. ELS *)
  3406. (* not needed any more as values are stored in the expression
  3407. IF symbol IS SyntaxTree.Constant THEN
  3408. result := symbol(SyntaxTree.Constant).value
  3409. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3410. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3411. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3412. ELSE
  3413. result := symbol(SyntaxTree.Constant).value
  3414. END;
  3415. ELSE
  3416. *)
  3417. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3418. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3419. THEN
  3420. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3421. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3422. left := NewDereferenceDesignator(position,left);
  3423. left.SetHidden(TRUE);
  3424. END;
  3425. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3426. scope := currentScope;
  3427. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3428. scope := scope.outerScope;
  3429. END;
  3430. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3431. Error(position, "forbidden access to symbol in parent procedure scope");
  3432. END;
  3433. END;
  3434. 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);
  3435. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3436. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3437. ELSE
  3438. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3439. END;
  3440. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3441. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3442. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3443. result.SetType(symbol.type);
  3444. result.SetAssignable(assignable);
  3445. symbol.MarkUsed;
  3446. IF symbol IS SyntaxTree.Constant THEN
  3447. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3448. END;
  3449. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3450. variableAccessed := TRUE
  3451. END;
  3452. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3453. IF GetGuard(symbol,guardType) THEN
  3454. IF (symbol.type.resolved IS SyntaxTree.RecordType) OR ~assignable THEN (* type guard is tested and type cannot be changed *)
  3455. result.SetType(guardType);
  3456. ELSE
  3457. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3458. END;
  3459. END;
  3460. END;
  3461. ASSERT(result.type # NIL);
  3462. RETURN result
  3463. END NewSymbolDesignator;
  3464. (** check and resolve an identifier designator "identifier"
  3465. - if identifier = self then return SelfDesignator
  3466. - else find symbol in current scope
  3467. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3468. **)
  3469. PROCEDURE VisitIdentifierDesignator*(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3470. VAR symbol: SyntaxTree.Symbol;
  3471. BEGIN
  3472. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3473. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3474. IF symbol # NIL THEN
  3475. ResolveSymbol(symbol);
  3476. ASSERT(symbol.type # NIL);
  3477. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3478. ELSE
  3479. Error(identifierDesignator.position,"Undeclared Identifier");
  3480. IF VerboseErrorMessage THEN
  3481. Printout.Info("undeclared identifier designator",identifierDesignator);
  3482. END;
  3483. resolvedExpression := SyntaxTree.invalidDesignator;
  3484. END;
  3485. END VisitIdentifierDesignator;
  3486. (** check and resolve a selector designator of the form left.designator
  3487. - if left is a pointer type then do auto dereferenciation
  3488. - left denotes a search scope:
  3489. - if left type is import type then set search scope to respective module
  3490. - if left type is enumeration type then set search scope to respective enumeration scope
  3491. - elsif left type is record type then set search scope to record scope
  3492. - search symbol in computed scope
  3493. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3494. **)
  3495. PROCEDURE VisitSelectorDesignator*(selectorDesignator: SyntaxTree.SelectorDesignator);
  3496. VAR
  3497. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3498. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3499. BEGIN
  3500. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3501. left := ResolveDesignator(selectorDesignator.left);
  3502. result := SyntaxTree.invalidDesignator;
  3503. IF left # NIL THEN
  3504. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3505. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3506. END;
  3507. scope := NIL;
  3508. IF left.type = NIL THEN
  3509. Error(selectorDesignator.position,"field on nil typed designator");
  3510. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3511. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3512. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3513. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3514. module := symbol(SyntaxTree.Import).module;
  3515. IF module # NIL THEN
  3516. scope := module.moduleScope
  3517. ELSE
  3518. Error(left.position,"module not loaded");
  3519. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3520. END;
  3521. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3522. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3523. ASSERT(scope # NIL)
  3524. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3525. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3526. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3527. IF type IS SyntaxTree.EnumerationType THEN
  3528. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3529. ELSE
  3530. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3531. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3532. END;
  3533. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3534. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3535. ELSE
  3536. Error(selectorDesignator.position,"field on non-record type designator");
  3537. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3538. END;
  3539. symbol := NIL;
  3540. IF scope # NIL THEN
  3541. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3542. IF symbol # NIL THEN
  3543. ResolveSymbol(symbol);
  3544. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3545. symbol.MarkUsed
  3546. ELSE
  3547. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3548. IF VerboseErrorMessage THEN
  3549. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3550. Printout.Info("scope", scope);
  3551. Printout.Info("left", left);
  3552. Printout.Info("undeclared identifier",selectorDesignator);
  3553. Printout.Info("left resolved designator",left);
  3554. END
  3555. END;
  3556. END;
  3557. END;
  3558. resolvedExpression := result;
  3559. END VisitSelectorDesignator;
  3560. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3561. VAR len,idx: Basic.Integer;
  3562. BEGIN
  3563. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3564. IF idx < 0 THEN
  3565. Error(index.position,"index out of bounds (too small)")
  3566. ELSE
  3567. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3568. Error(index.position,"index out of bounds (too large)");
  3569. END;
  3570. END;
  3571. END;
  3572. END IndexCheck;
  3573. (*
  3574. - if index designator has not type, use newBaseType as its type
  3575. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3576. - special rule: if static array of dynamic array occurs, make it all dynamic
  3577. index designator type: new base type: new index designator type:
  3578. NIL z z
  3579. ARRAY [x, y] z ARRAY [x, y] OF z
  3580. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3581. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3582. *)
  3583. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3584. VAR
  3585. mathArrayType: SyntaxTree.MathArrayType;
  3586. makeDynamic: BOOLEAN;
  3587. BEGIN
  3588. IF indexDesignator.type = NIL THEN
  3589. indexDesignator.SetType(newBaseType)
  3590. ELSE
  3591. (* index designator must be a of math array type *)
  3592. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3593. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3594. (* determine if all arrays have to be made dynamic *)
  3595. makeDynamic :=
  3596. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3597. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3598. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3599. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3600. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3601. END;
  3602. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3603. mathArrayType.SetArrayBase(newBaseType)
  3604. END
  3605. END SetIndexBaseType;
  3606. (** check and append index list element to index designator of math array
  3607. - check validity of single index or array range
  3608. - compute new type
  3609. - if range then create new array type (calculate length of resulting array)
  3610. - otherwise take sourceArray.arrayBase as new type
  3611. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3612. **)
  3613. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3614. VAR
  3615. targetArray: SyntaxTree.MathArrayType;
  3616. first, last, step: SyntaxTree.Expression;
  3617. firstValue, lastValue, stepValue, length: Basic.Integer;
  3618. rangeExpression: SyntaxTree.RangeExpression;
  3619. BEGIN
  3620. IF indexListItem.type = SyntaxTree.invalidType THEN
  3621. (* error already handled *)
  3622. indexDesignator.parameters.AddExpression(indexListItem)
  3623. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3624. indexDesignator.HasRange;
  3625. indexDesignator.HasTensorRange;
  3626. indexDesignator.parameters.AddExpression(indexListItem);
  3627. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3628. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3629. IndexCheck(indexListItem, sourceArray.length);
  3630. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3631. indexDesignator.parameters.AddExpression(indexListItem)
  3632. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3633. indexDesignator.HasRange;
  3634. (* if the range is given as an array range expression, check the validity of its components *)
  3635. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3636. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3637. first := rangeExpression.first;
  3638. last := rangeExpression.last;
  3639. step := rangeExpression.step;
  3640. (* perform static checks on range components *)
  3641. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3642. Error(indexListItem.position,"lower bound of array range too small")
  3643. END;
  3644. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3645. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3646. Error(indexListItem.position,"upper bound of array range too large")
  3647. END
  3648. END;
  3649. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3650. Error(indexListItem.position,"invalid step size")
  3651. END;
  3652. (* add conversions to size type *)
  3653. (* TODO: needed? *)
  3654. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3655. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3656. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3657. END;
  3658. IF indexDesignator.hasTensorRange THEN
  3659. (* the index designator's base type is a tensor: leave it as is *)
  3660. ELSE
  3661. (* append a new math array to the index designator's base type *)
  3662. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3663. IF ~error THEN
  3664. (*
  3665. (* optimization: calculate length of target array for static ranges *)
  3666. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3667. IF IsStaticallyOpenRange(rangeExpression) THEN
  3668. (* range is open ('*'): reuse source array length as target array length *)
  3669. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3670. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3671. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3672. IF lastValue = MAX(LONGINT) THEN
  3673. IF IsIntegerValue(sourceArray.length, length) THEN
  3674. lastValue := length - 1;
  3675. isStaticTargetArrayLength := TRUE
  3676. ELSE
  3677. isStaticTargetArrayLength := FALSE
  3678. END
  3679. ELSE
  3680. isStaticTargetArrayLength := TRUE
  3681. END;
  3682. IF isStaticTargetArrayLength THEN
  3683. (* calculate static target array length *)
  3684. IF firstValue > lastValue THEN
  3685. length := 0
  3686. ELSE
  3687. length := 1 + lastValue - firstValue;
  3688. IF length MOD stepValue = 0 THEN
  3689. length := length DIV stepValue
  3690. ELSE
  3691. length := length DIV stepValue + 1
  3692. END
  3693. END;
  3694. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3695. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3696. ASSERT(targetArray.form = SyntaxTree.Static)
  3697. END
  3698. END
  3699. END
  3700. *)
  3701. END;
  3702. SetIndexBaseType(indexDesignator, targetArray)
  3703. END;
  3704. indexDesignator.parameters.AddExpression(indexListItem)
  3705. ELSE
  3706. Error(position,"invalid index list item");
  3707. END;
  3708. END AppendMathIndex;
  3709. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3710. VAR parameters: SyntaxTree.ExpressionList;
  3711. BEGIN
  3712. parameters := index.parameters;
  3713. IF (expression.type = NIL) THEN
  3714. Error(position, "invalid index");
  3715. ELSIF IsIntegerType(expression.type.resolved) THEN
  3716. IF over IS SyntaxTree.ArrayType THEN
  3717. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3718. ELSIF over IS SyntaxTree.StringType THEN
  3719. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3720. END;
  3721. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3722. parameters.AddExpression(expression);
  3723. ELSE
  3724. Error(position, "invalid index");
  3725. END;
  3726. END AppendIndex;
  3727. (** convert an expression to math array type
  3728. if expression is of math array type: return expression itself
  3729. if expression is of array-structured object type: return an index operator call on it
  3730. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3731. otherwise: return invalid expression
  3732. **)
  3733. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3734. VAR
  3735. result: SyntaxTree.Expression;
  3736. mathArrayType: SyntaxTree.MathArrayType;
  3737. BEGIN
  3738. IF expression.type = NIL THEN
  3739. result := SyntaxTree.invalidExpression
  3740. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3741. (* expression of math array type *)
  3742. result := expression
  3743. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3744. (* expression of array-structured object type *)
  3745. mathArrayType := MathArrayStructureOfType(expression.type);
  3746. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3747. ELSE
  3748. result := SyntaxTree.invalidExpression
  3749. END;
  3750. RETURN result
  3751. END ConvertToMathArray;
  3752. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3753. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3754. VAR
  3755. result: SyntaxTree.ExpressionList;
  3756. i: LONGINT;
  3757. BEGIN
  3758. result := SyntaxTree.NewExpressionList();
  3759. FOR i := 1 TO itemCount DO
  3760. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3761. END;
  3762. RETURN result
  3763. END ListOfOpenRanges;
  3764. (** create a procedure call designator for an index operator call on an array-structured object type
  3765. - use given index list as actual parameters
  3766. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3767. **)
  3768. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3769. VAR
  3770. operator: SyntaxTree.Operator;
  3771. expression: SyntaxTree.Expression;
  3772. actualParameters, tempList: SyntaxTree.ExpressionList;
  3773. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3774. result, tempDesignator: SyntaxTree.Designator;
  3775. recordType: SyntaxTree.RecordType;
  3776. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3777. i, hashValue, indexListSize, indexListKind: LONGINT;
  3778. castReturnType: SyntaxTree.MathArrayType;
  3779. BEGIN
  3780. ASSERT(IsArrayStructuredObjectType(left.type));
  3781. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3782. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3783. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3784. indexListSize := indexList.Length();
  3785. indexListKind := 0;
  3786. containsNonRange := FALSE;
  3787. FOR i := 0 TO indexList.Length() - 1 DO
  3788. indexListKind := indexListKind * 2;
  3789. expression := indexList.GetExpression(i);
  3790. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3791. INC(indexListKind)
  3792. ELSE
  3793. containsNonRange := TRUE
  3794. END
  3795. END;
  3796. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3797. (* select applicable index operator
  3798. - try to look up optimal index operator
  3799. - if not present, use operator on ranges
  3800. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3801. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3802. *)
  3803. usesGeneralOperator := FALSE;
  3804. IF rhs # NIL THEN
  3805. (* write operator *)
  3806. IF hashValue = -1 THEN
  3807. operator := NIL
  3808. ELSE
  3809. operator := recordType.arrayAccessOperators.write[hashValue];
  3810. END;
  3811. IF operator = NIL THEN
  3812. usesPureRangeOperator := TRUE;
  3813. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3814. operator := recordType.arrayAccessOperators.generalWrite;
  3815. usesGeneralOperator := TRUE
  3816. ELSE
  3817. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3818. operator := recordType.arrayAccessOperators.write[hashValue];
  3819. END
  3820. END
  3821. ELSE
  3822. (* read operator *)
  3823. IF hashValue = -1 THEN
  3824. operator := NIL
  3825. ELSE
  3826. operator := recordType.arrayAccessOperators.read[hashValue];
  3827. END;
  3828. IF operator = NIL THEN
  3829. usesPureRangeOperator := TRUE;
  3830. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3831. operator := recordType.arrayAccessOperators.generalRead;
  3832. usesGeneralOperator := TRUE
  3833. ELSE
  3834. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3835. operator := recordType.arrayAccessOperators.read[hashValue];
  3836. END
  3837. END
  3838. END;
  3839. IF operator = NIL THEN
  3840. Error(position, "call of undeclared [] operator");
  3841. result := SyntaxTree.invalidDesignator;
  3842. ELSE
  3843. (* determine if reshaping is needed *)
  3844. needsReshaping := containsNonRange & usesPureRangeOperator;
  3845. (* import OCArrayBase if reshaping is needed *)
  3846. IF needsReshaping & ~arrayBaseImported THEN
  3847. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3848. arrayBaseImported := TRUE
  3849. END;
  3850. (* add the index list item to the list of actual parameters
  3851. - for general operators: add a single inline array containing the index list items as parameter
  3852. - otherwise: add all index list items as individual parameters
  3853. *)
  3854. actualParameters := SyntaxTree.NewExpressionList();
  3855. IF usesGeneralOperator THEN
  3856. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3857. END;
  3858. FOR i := 0 TO indexListSize - 1 DO
  3859. expression := indexList.GetExpression(i);
  3860. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3861. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3862. tempList := SyntaxTree.NewExpressionList();
  3863. tempList.AddExpression(expression);
  3864. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3865. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3866. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3867. END;
  3868. IF usesGeneralOperator THEN
  3869. tempMathArrayExpression.elements.AddExpression(expression);
  3870. ELSE
  3871. actualParameters.AddExpression(expression)
  3872. END
  3873. END;
  3874. IF usesGeneralOperator THEN
  3875. actualParameters.AddExpression(tempMathArrayExpression)
  3876. END;
  3877. IF rhs # NIL THEN
  3878. (* add actual parameter for RHS *)
  3879. IF needsReshaping THEN
  3880. (* reshape using OCArrayBase.ExpandDimensions *)
  3881. tempList := SyntaxTree.NewExpressionList();
  3882. (* source array *)
  3883. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3884. tempList.AddExpression(rhs);
  3885. ELSE
  3886. (* convert scalar to one-dimensional array *)
  3887. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3888. tempMathArrayExpression.elements.AddExpression(rhs);
  3889. tempList.AddExpression(tempMathArrayExpression)
  3890. END;
  3891. (* list of kept dimensions *)
  3892. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3893. FOR i := 0 TO indexListSize - 1 DO
  3894. expression := indexList.GetExpression(i);
  3895. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3896. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3897. ELSE
  3898. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3899. END
  3900. END;
  3901. tempList.AddExpression(tempMathArrayExpression);
  3902. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3903. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3904. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3905. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3906. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3907. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3908. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3909. expression.SetType(castReturnType);
  3910. ELSE
  3911. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3912. END;
  3913. actualParameters.AddExpression(expression)
  3914. ELSE
  3915. actualParameters.AddExpression(rhs)
  3916. END
  3917. END;
  3918. (* add dereference operator and create procedure call designator *)
  3919. ASSERT(left IS SyntaxTree.Designator);
  3920. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3921. ASSERT(expression IS SyntaxTree.Designator);
  3922. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3923. IF (rhs = NIL) & needsReshaping THEN
  3924. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3925. tempList := SyntaxTree.NewExpressionList();
  3926. FOR i := 0 TO indexList.Length() - 1 DO
  3927. expression := indexList.GetExpression(i);
  3928. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3929. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3930. ELSE
  3931. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3932. END
  3933. END;
  3934. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3935. END;
  3936. IF rhs = NIL THEN
  3937. (* special rule: index read operator calls are considered to be assignable *)
  3938. result.SetAssignable(TRUE)
  3939. END;
  3940. (* put information about this index operator call into the resulting designator *)
  3941. result.SetRelatedAsot(left);
  3942. result.SetRelatedIndexList(indexList)
  3943. END;
  3944. RETURN result
  3945. END NewIndexOperatorCall;
  3946. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3947. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3948. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3949. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3950. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3951. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3952. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3953. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3954. CONST trace = FALSE;
  3955. BEGIN
  3956. IF trace THEN
  3957. FOR i := 0 TO actualParameters.Length()-1 DO
  3958. Printout.Info("par", actualParameters.GetExpression(i));
  3959. END;
  3960. END;
  3961. operator := scope.firstOperator;
  3962. WHILE(operator # NIL) DO
  3963. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3964. procedureType := operator.type(SyntaxTree.ProcedureType);
  3965. distance := Distance(system, procedureType,actualParameters);
  3966. IF trace THEN Printout.Info("check op ",operator) END;
  3967. IF distance < bestDistance THEN
  3968. IF trace THEN Printout.Info("taken op",operator) END;
  3969. bestDistance := distance;
  3970. bestOperator := operator;
  3971. END;
  3972. END;
  3973. operator := operator.nextOperator;
  3974. END;
  3975. END FindInScope;
  3976. BEGIN
  3977. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3978. IF oper = 0 THEN (* index *)
  3979. identifier := SyntaxTree.NewIdentifier("[]");
  3980. ELSE
  3981. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3982. END;
  3983. WHILE (recordType # NIL) DO
  3984. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3985. recordType := recordType.GetBaseRecord();
  3986. END;
  3987. RETURN bestOperator
  3988. END FindOperator;
  3989. BEGIN
  3990. type := left.type.resolved;
  3991. IF type IS SyntaxTree.RecordType THEN
  3992. pointer := FALSE;
  3993. recordType := type(SyntaxTree.RecordType);
  3994. ELSE
  3995. pointer := TRUE;
  3996. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3997. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3998. END;
  3999. actualParameters := SyntaxTree.NewExpressionList();
  4000. IF parameters # NIL THEN
  4001. FOR i := 0 TO parameters.Length()-1 DO
  4002. expression := ResolveExpression(parameters.GetExpression(i));
  4003. actualParameters.AddExpression(expression);
  4004. END;
  4005. END;
  4006. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  4007. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4008. IF op # NIL THEN
  4009. designator := left(SyntaxTree.Designator);
  4010. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  4011. expression := NewSymbolDesignator(position, designator , op);
  4012. ASSERT(expression IS SyntaxTree.Designator);
  4013. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  4014. result.SetRelatedAsot(left);
  4015. result.SetRelatedIndexList(parameters);
  4016. (* check if write operator exists, for var parameters *)
  4017. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  4018. actualParameters := SyntaxTree.NewExpressionList();
  4019. FOR i := 0 TO parameters.Length()-1 DO
  4020. expression := ResolveExpression(parameters.GetExpression(i));
  4021. actualParameters.AddExpression(expression);
  4022. END;
  4023. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  4024. actualParameters.AddExpression(rhs);
  4025. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4026. IF op = NIL THEN rhs := NIL END;
  4027. END;
  4028. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  4029. ELSE
  4030. result := NIL;
  4031. END;
  4032. RETURN result;
  4033. END NewObjectOperatorCall;
  4034. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  4035. 1. convert bracket designator chains into a single one that contains separators
  4036. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  4037. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  4038. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  4039. - if an array or math array is indexed over, create index designator
  4040. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  4041. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4042. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4043. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4044. - if an array-structured object type is indexed over, create procedure call designator
  4045. e.g.: a[x, y] -> a^."[]"(x, y)
  4046. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4047. - a[i, *] = a[i][*]
  4048. - a[*, i] # a[*][i]
  4049. Because:
  4050. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4051. - 'i-th column' = a[*, i]
  4052. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4053. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4054. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4055. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4056. **)
  4057. PROCEDURE VisitBracketDesignator*(bracketDesignator: SyntaxTree.BracketDesignator);
  4058. VAR
  4059. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4060. indexDesignator: SyntaxTree.IndexDesignator;
  4061. designator: SyntaxTree.Designator;
  4062. type: SyntaxTree.Type;
  4063. recordType: SyntaxTree.RecordType;
  4064. expression, rhs: SyntaxTree.Expression;
  4065. indexList: SyntaxTree.ExpressionList;
  4066. i: LONGINT;
  4067. hasError, done: BOOLEAN;
  4068. PROCEDURE FinalizeIndexDesignator;
  4069. BEGIN
  4070. IF indexDesignator # NIL THEN
  4071. (* the end of a tensor has been reached: *)
  4072. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4073. SetIndexBaseType(indexDesignator, type);
  4074. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4075. designator := indexDesignator;
  4076. type := designator.type.resolved;
  4077. indexDesignator := NIL;
  4078. ASSERT(SyntaxTree.Resolved IN type.state)
  4079. END
  4080. END FinalizeIndexDesignator;
  4081. BEGIN
  4082. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4083. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4084. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4085. (* copy all index list entries including a separator to the left bracket designator *)
  4086. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4087. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4088. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4089. END;
  4090. (* propagate the related RHS *)
  4091. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4092. (* only resolve left bracket designator and use as final result *)
  4093. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4094. ELSE
  4095. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4096. designator := ResolveDesignator(bracketDesignator.left);
  4097. type := designator.type.resolved;
  4098. indexDesignator := NIL;
  4099. (*!!! clean up *)
  4100. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4101. OR (type IS SyntaxTree.RecordType)
  4102. THEN
  4103. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4104. IF resolvedExpression = NIL THEN
  4105. Error(bracketDesignator.position,"undefined operator");
  4106. resolvedExpression := SyntaxTree.invalidDesignator
  4107. END;
  4108. RETURN;
  4109. END;
  4110. i := 0;
  4111. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4112. expression := bracketDesignator.parameters.GetExpression(i);
  4113. expression := ResolveExpression(expression);
  4114. bracketDesignator.parameters.SetExpression(i, expression);
  4115. IF expression = SyntaxTree.indexListSeparator THEN
  4116. (* finalize an existing index designator if needed *)
  4117. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4118. INC(i)
  4119. ELSE
  4120. (* do auto-dereferencing if needed *)
  4121. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4122. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4123. & (i=0)*)
  4124. THEN
  4125. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4126. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4127. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4128. designator := SyntaxTree.invalidDesignator;
  4129. type := SyntaxTree.invalidType
  4130. ELSE
  4131. FinalizeIndexDesignator;
  4132. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4133. type := designator.type.resolved
  4134. END
  4135. END;
  4136. (* create a new index designator, if needed *)
  4137. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4138. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4139. indexDesignator.SetAssignable(designator.assignable);
  4140. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4141. (* designator := indexDesignator *)
  4142. END;
  4143. IF type = SyntaxTree.invalidType THEN
  4144. (* error already handled *)
  4145. INC(i)
  4146. ELSIF type IS SyntaxTree.ArrayType THEN
  4147. (* indexing over an array *)
  4148. ASSERT(indexDesignator # NIL);
  4149. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4150. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4151. INC(i)
  4152. ELSIF type IS SyntaxTree.StringType THEN
  4153. (* indexing over an array *)
  4154. ASSERT(indexDesignator # NIL);
  4155. AppendIndex(expression.position, indexDesignator, expression, type);
  4156. type := type(SyntaxTree.StringType).baseType.resolved;
  4157. INC(i)
  4158. ELSIF type IS SyntaxTree.MathArrayType THEN
  4159. (* indexing over a math array *)
  4160. ASSERT(indexDesignator # NIL);
  4161. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4162. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4163. INC(i)
  4164. ELSIF IsArrayStructuredObjectType(type) THEN
  4165. (* indexing over ASOTs *)
  4166. FinalizeIndexDesignator;
  4167. ASSERT(type IS SyntaxTree.PointerType);
  4168. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4169. (*
  4170. - collect index list items from bracket designator that belong to ASOT
  4171. - check for errors
  4172. *)
  4173. indexList := SyntaxTree.NewExpressionList();
  4174. hasError := FALSE;
  4175. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4176. (* indexing over tensor ASOT:
  4177. - stop at index list end or separator
  4178. - dimensionality is given by number of index list items
  4179. *)
  4180. done := FALSE;
  4181. WHILE ~done DO
  4182. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4183. done := TRUE;
  4184. ELSE
  4185. expression := bracketDesignator.parameters.GetExpression(i);
  4186. IF expression = SyntaxTree.indexListSeparator THEN
  4187. done := TRUE;
  4188. ELSE
  4189. expression := ResolveExpression(expression);
  4190. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4191. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4192. hasError := TRUE
  4193. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4194. Error(expression.position, "integer or range expected");
  4195. expression := SyntaxTree.invalidExpression;
  4196. hasError := TRUE
  4197. END;
  4198. indexList.AddExpression(expression)
  4199. END;
  4200. INC(i)
  4201. END
  4202. END
  4203. ELSE
  4204. (* indexing over non-tensor ASOT:
  4205. - ignore separators
  4206. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4207. *)
  4208. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4209. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4210. expression := bracketDesignator.parameters.GetExpression(i);
  4211. ELSE
  4212. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4213. END;
  4214. IF expression # SyntaxTree.indexListSeparator THEN
  4215. expression := ResolveExpression(expression);
  4216. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4217. Error(expression.position, "integer or range expected");
  4218. expression := SyntaxTree.invalidExpression;
  4219. hasError := TRUE
  4220. END;
  4221. indexList.AddExpression(expression)
  4222. END;
  4223. INC(i)
  4224. END;
  4225. END;
  4226. IF hasError THEN
  4227. designator := SyntaxTree.invalidDesignator;
  4228. type := SyntaxTree.invalidType;
  4229. ELSE
  4230. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4231. and the last entry in the index list belongs to the array-structured object type in question.
  4232. E.g.: for a 2-dimensional array-structured object type:
  4233. - 'lhs := asot[1, 2]' -> read mode
  4234. - 'asot[1, 2] := rhs' -> write mode
  4235. - 'asot[1, 2, 3] := rhs' -> read mode
  4236. *)
  4237. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4238. rhs := bracketDesignator.relatedRhs
  4239. ELSE
  4240. rhs := NIL
  4241. END;
  4242. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4243. type := designator.type
  4244. END
  4245. ELSE
  4246. Error(expression.position,"indexing over non-array type");
  4247. designator := SyntaxTree.invalidDesignator;
  4248. type := SyntaxTree.invalidType;
  4249. INC(i)
  4250. END
  4251. END
  4252. END;
  4253. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4254. resolvedExpression := designator
  4255. END
  4256. END VisitBracketDesignator;
  4257. (** check and resolve expression list
  4258. - resolve each expression in an expression list
  4259. - returns true if and only if all statements could have successfully been resolved
  4260. **)
  4261. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4262. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4263. BEGIN
  4264. result := TRUE;
  4265. FOR i := 0 TO expressionList.Length()-1 DO
  4266. expression := ResolveExpression(expressionList.GetExpression(i));
  4267. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4268. expressionList.SetExpression(i,expression);
  4269. END;
  4270. RETURN result
  4271. END ExpressionList;
  4272. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4273. BEGIN
  4274. type := type.resolved;
  4275. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4276. RETURN TRUE
  4277. ELSIF system.CanPassInRegister # NIL THEN
  4278. RETURN system.CanPassInRegister(type);
  4279. ELSE
  4280. RETURN FALSE
  4281. END;
  4282. END CanPassInRegister;
  4283. (** return procedure call designator left(actualParameters)
  4284. - check realtime procedure call in realtime procedure
  4285. - check number of parameters
  4286. - check parameter compatibility
  4287. return invalidDesignator if error
  4288. **)
  4289. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4290. VAR result: SyntaxTree.Designator;
  4291. numberFormalParameters, numberActualParameters: LONGINT;
  4292. formalType: SyntaxTree.ProcedureType;
  4293. formalParameter: SyntaxTree.Parameter;
  4294. actualParameter: SyntaxTree.Expression;
  4295. i: LONGINT;
  4296. self: SyntaxTree.Expression;
  4297. BEGIN
  4298. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4299. result := SyntaxTree.invalidDesignator;
  4300. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4301. numberFormalParameters := formalType.numberParameters;
  4302. numberActualParameters := actualParameters.Length();
  4303. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4304. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4305. END;
  4306. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4307. self := left.left;
  4308. IF (self # NIL) & ~IsVariable(self) THEN
  4309. Error(self.position, "Non-variable expression on variable receiver");
  4310. END;
  4311. END;
  4312. IF ~ExpressionList(actualParameters) THEN
  4313. result := SyntaxTree.invalidDesignator
  4314. ELSE
  4315. IF numberActualParameters <= numberFormalParameters THEN
  4316. formalParameter := formalType.firstParameter;
  4317. FOR i := 0 TO numberActualParameters-1 DO
  4318. actualParameter := actualParameters.GetExpression(i);
  4319. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4320. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4321. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4322. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4323. ELSE
  4324. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4325. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4326. END;
  4327. actualParameters.SetExpression(i,actualParameter);
  4328. END;
  4329. formalParameter := formalParameter.nextParameter;
  4330. END;
  4331. WHILE (formalParameter # NIL) DO
  4332. IF formalParameter.defaultValue # NIL THEN
  4333. actualParameters.AddExpression(formalParameter.defaultValue);
  4334. formalParameter := formalParameter.nextParameter
  4335. ELSE
  4336. Error(position, "less actual than formal parameters");
  4337. formalParameter := NIL;
  4338. END;
  4339. END;
  4340. ELSE
  4341. Error(position, "more actual than formal parameters")
  4342. END;
  4343. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4344. result.SetAssignable(FALSE);
  4345. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4346. END;
  4347. RETURN result
  4348. END NewProcedureCallDesignator;
  4349. (**
  4350. builtin call designator generated in VisitParameterDesignator
  4351. -> nothing to be resolved
  4352. **)
  4353. PROCEDURE VisitTypeGuardDesignator*(x: SyntaxTree.TypeGuardDesignator);
  4354. BEGIN
  4355. resolvedExpression := x;
  4356. END VisitTypeGuardDesignator;
  4357. (**
  4358. builtin call designator generated in VisitParameterDesignator
  4359. -> nothing to be resolved
  4360. **)
  4361. PROCEDURE VisitBuiltinCallDesignator*(x: SyntaxTree.BuiltinCallDesignator);
  4362. BEGIN
  4363. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4364. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4365. ASSERT(resolvedExpression.type # NIL);
  4366. ELSIF ExpressionList(x.parameters) THEN
  4367. resolvedExpression := x;
  4368. END;
  4369. END VisitBuiltinCallDesignator;
  4370. (**
  4371. procedure call designator generated in VisitParameterDesignator
  4372. -> nothing to be resolved
  4373. **)
  4374. PROCEDURE VisitProcedureCallDesignator*(x: SyntaxTree.ProcedureCallDesignator);
  4375. BEGIN
  4376. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4377. resolvedExpression := x;
  4378. END VisitProcedureCallDesignator;
  4379. (** return true if x is a variable else return false and report error **)
  4380. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4381. VAR result: BOOLEAN;
  4382. BEGIN
  4383. result := TRUE;
  4384. IF x = SyntaxTree.invalidExpression THEN
  4385. result := FALSE;
  4386. ELSIF ~IsVariable(x) THEN
  4387. Error(x.position,"non variable expression");
  4388. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4389. result := FALSE;
  4390. END;
  4391. RETURN result
  4392. END CheckVariable;
  4393. (**
  4394. if expression x is of basic type then return true else report error and return false
  4395. **)
  4396. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4397. VAR result: BOOLEAN;
  4398. BEGIN
  4399. result := FALSE;
  4400. IF x = SyntaxTree.invalidExpression THEN
  4401. ELSIF ~IsBasicType(x.type) THEN
  4402. Error(x.position,"is no basic type");
  4403. result := FALSE
  4404. ELSE result := TRUE
  4405. END;
  4406. RETURN result
  4407. END CheckBasicType;
  4408. (**
  4409. if expression x is of number type then return true else report error and return false
  4410. **)
  4411. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4412. VAR result: BOOLEAN;
  4413. BEGIN
  4414. result := FALSE;
  4415. IF x = SyntaxTree.invalidExpression THEN
  4416. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4417. Error(x.position,"is non number type");
  4418. ELSE result := TRUE
  4419. END;
  4420. RETURN result
  4421. END CheckNumberType;
  4422. (**
  4423. if expression x is of number or size type but not complex then return true else report error and return false
  4424. **)
  4425. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4426. VAR result: BOOLEAN;
  4427. BEGIN
  4428. result := FALSE;
  4429. IF x = SyntaxTree.invalidExpression THEN
  4430. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4431. Error(x.position,"is complex type");
  4432. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4433. Error(x.position,"is non number type");
  4434. ELSE result := TRUE
  4435. END;
  4436. RETURN result
  4437. END CheckNonComplexNumberSizeType;
  4438. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4439. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4440. BEGIN
  4441. result := FALSE; type := x.type.resolved;
  4442. IF x = SyntaxTree.invalidExpression THEN
  4443. 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
  4444. TRACE(type.sizeInBits);
  4445. TRACE(system.addressType.sizeInBits);
  4446. Error(x.position,"is no address type");
  4447. ELSE result := TRUE
  4448. END;
  4449. RETURN result
  4450. END CheckAddressType;
  4451. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4452. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4453. BEGIN
  4454. result := FALSE; type := x.type.resolved;
  4455. IF x = SyntaxTree.invalidExpression THEN
  4456. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4457. Error(x.position,"is no size type");
  4458. ELSE result := TRUE
  4459. END;
  4460. RETURN result
  4461. END CheckSizeType;
  4462. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4463. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4464. BEGIN
  4465. result := FALSE; type := x.type.resolved;
  4466. IF x = SyntaxTree.invalidExpression THEN
  4467. 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
  4468. Error(x.position,"is no object type");
  4469. ELSE result := TRUE
  4470. END;
  4471. RETURN result
  4472. END CheckObjectType;
  4473. (**
  4474. if expression x is of integer type then return true else report error and return false
  4475. **)
  4476. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4477. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4478. BEGIN
  4479. result := FALSE; type := x.type.resolved;
  4480. IF x = SyntaxTree.invalidExpression THEN
  4481. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4482. Error(x.position,"is no integer type");
  4483. ELSE result := TRUE
  4484. END;
  4485. RETURN result
  4486. END CheckIntegerType;
  4487. (**
  4488. if expression x is of character type then return true else report error and return false
  4489. **)
  4490. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4491. VAR result: BOOLEAN;
  4492. BEGIN
  4493. result := FALSE;
  4494. IF x = SyntaxTree.invalidExpression THEN
  4495. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4496. Error(x.position,"is no character type");
  4497. ELSE result := TRUE
  4498. END;
  4499. RETURN result
  4500. END CheckCharacterType;
  4501. (**
  4502. if expression x is of real type then return true else report error and return false
  4503. **)
  4504. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4505. VAR result: BOOLEAN;
  4506. BEGIN
  4507. result := FALSE;
  4508. IF x = SyntaxTree.invalidExpression THEN
  4509. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4510. Error(x.position,"is no float type");
  4511. ELSE result := TRUE
  4512. END;
  4513. RETURN result
  4514. END CheckRealType;
  4515. (**
  4516. if expression x is of range type then return true else report error and return false
  4517. **)
  4518. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4519. VAR result: BOOLEAN;
  4520. BEGIN
  4521. result := FALSE;
  4522. IF x = SyntaxTree.invalidExpression THEN
  4523. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4524. Error(x.position,"is no range type");
  4525. ELSE result := TRUE
  4526. END;
  4527. RETURN result
  4528. END CheckRangeType;
  4529. (**
  4530. if expression x is of boolean type then return true else report error and return false
  4531. **)
  4532. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4533. VAR result: BOOLEAN;
  4534. BEGIN
  4535. result := FALSE;
  4536. IF x = SyntaxTree.invalidExpression THEN
  4537. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4538. Error(x.position,"is no boolean type");
  4539. ELSE result := TRUE
  4540. END;
  4541. RETURN result
  4542. END CheckBooleanType;
  4543. (**
  4544. if expression x is of set type then return true else report error and return false
  4545. **)
  4546. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4547. VAR result: BOOLEAN;
  4548. BEGIN
  4549. result := FALSE;
  4550. IF x = SyntaxTree.invalidExpression THEN
  4551. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4552. Error(x.position,"is no set type");
  4553. ELSE result := TRUE
  4554. END;
  4555. RETURN result
  4556. END CheckSetType;
  4557. (**
  4558. if expression x is of string or array of character type then return true else report error and return false
  4559. **)
  4560. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4561. VAR result: BOOLEAN;
  4562. BEGIN
  4563. result := FALSE;
  4564. IF x = SyntaxTree.invalidExpression THEN
  4565. ELSIF ~IsStringType(x.type.resolved) THEN
  4566. Error(x.position,"is no string type");
  4567. ELSE result := TRUE
  4568. END;
  4569. RETURN result
  4570. END CheckStringType;
  4571. (**
  4572. if expression x is a type declaration type return true else report error and return false
  4573. **)
  4574. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4575. VAR result: BOOLEAN;
  4576. BEGIN
  4577. result := FALSE;
  4578. IF x = SyntaxTree.invalidExpression THEN
  4579. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4580. Error(x.position,"is not a type declaration");
  4581. ELSE result := TRUE
  4582. END;
  4583. RETURN result
  4584. END CheckTypeDeclarationType;
  4585. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4586. VAR result: BOOLEAN;
  4587. BEGIN
  4588. result := FALSE;
  4589. IF x = SyntaxTree.invalidExpression THEN
  4590. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4591. result := TRUE;
  4592. value := x.resolved(SyntaxTree.IntegerValue).value;
  4593. ELSE
  4594. Error(x.position,"expression is not an integer constant");
  4595. END;
  4596. RETURN result;
  4597. END CheckIntegerValue;
  4598. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4599. VAR result: BOOLEAN;
  4600. BEGIN
  4601. result := FALSE;
  4602. IF x = SyntaxTree.invalidExpression THEN
  4603. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4604. result := TRUE;
  4605. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4606. ELSE
  4607. Error(x.position,"expression is not an integer constant");
  4608. END;
  4609. RETURN result;
  4610. END CheckStringValue;
  4611. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4612. BEGIN
  4613. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4614. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, maxSizeInBits)
  4615. ELSE
  4616. RETURN FALSE
  4617. END;
  4618. END IsUnsignedValue;
  4619. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4620. BEGIN
  4621. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4622. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, system.addressType.sizeInBits)
  4623. ELSE
  4624. RETURN FALSE
  4625. END
  4626. END IsAddressValue;
  4627. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4628. BEGIN
  4629. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4630. END IsAddressExpression;
  4631. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4632. BEGIN
  4633. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4634. END IsSizeExpression;
  4635. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4636. VAR result: BOOLEAN;
  4637. BEGIN
  4638. result := FALSE;
  4639. IF x = SyntaxTree.invalidExpression THEN
  4640. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4641. result := TRUE;
  4642. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4643. ELSE
  4644. Error(x.position,"expression is not an integer constant");
  4645. END;
  4646. RETURN result;
  4647. END CheckEnumerationValue;
  4648. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4649. VAR result: BOOLEAN;
  4650. BEGIN
  4651. result := FALSE;
  4652. IF x = SyntaxTree.invalidExpression THEN
  4653. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4654. result := TRUE;
  4655. value := x.resolved(SyntaxTree.CharacterValue).value;
  4656. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4657. result := TRUE;
  4658. value := x.resolved(SyntaxTree.StringValue).value[0];
  4659. ELSE
  4660. Error(x.position,"expression is not a character constant");
  4661. END;
  4662. RETURN result;
  4663. END CheckCharacterValue;
  4664. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer; includeZero: BOOLEAN): BOOLEAN;
  4665. VAR result: BOOLEAN;
  4666. BEGIN
  4667. result := FALSE;
  4668. IF x = SyntaxTree.invalidExpression THEN
  4669. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4670. value := x.resolved(SyntaxTree.IntegerValue).value;
  4671. IF (value > 0) OR includeZero & (value = 0) THEN
  4672. result := TRUE;
  4673. ELSE
  4674. Error(x.position,"integer is not positive");
  4675. END
  4676. ELSE
  4677. Error(x.position,"expression is not an integer constant");
  4678. END;
  4679. RETURN result;
  4680. END CheckPositiveIntegerValue;
  4681. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4682. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4683. BEGIN
  4684. result := FALSE;
  4685. IF x = SyntaxTree.invalidExpression THEN
  4686. ELSE
  4687. type := x.type.resolved;
  4688. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4689. portType := type(SyntaxTree.PortType);
  4690. result := TRUE
  4691. ELSE
  4692. Error(x.position,"no port type");
  4693. END;
  4694. END;
  4695. RETURN result
  4696. END CheckPortType;
  4697. (* move to builtin procedure call statement ?
  4698. remove builtin procedure call designator ?
  4699. *)
  4700. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4701. VAR
  4702. numberActualParameters,numberFormalParameters: LONGINT;
  4703. formalParameter: SyntaxTree.Parameter;
  4704. actualParameter: SyntaxTree.Expression;
  4705. procedureType: SyntaxTree.ProcedureType;
  4706. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4707. inPort, outPort: SyntaxTree.PortType;
  4708. constructor: SyntaxTree.Procedure;
  4709. type0,type1,type2: SyntaxTree.Type;
  4710. type,base,parameterType: SyntaxTree.Type;
  4711. arrayType: SyntaxTree.ArrayType;
  4712. i: LONGINT; i0, i1, value: Basic.Integer;
  4713. r,r0,r1,im: LONGREAL;
  4714. c: CHAR;
  4715. id: LONGINT;
  4716. b: BOOLEAN;
  4717. first: LONGINT;
  4718. mathArrayType: SyntaxTree.MathArrayType;
  4719. customBuiltin: SyntaxTree.CustomBuiltin;
  4720. PROCEDURE CheckArity(from,to: Basic.Integer): BOOLEAN;
  4721. VAR resultB: BOOLEAN;
  4722. BEGIN
  4723. IF numberActualParameters < from THEN
  4724. Error(position, "less actual than formal parameters");
  4725. result := SyntaxTree.invalidExpression;
  4726. resultB := FALSE;
  4727. ELSIF numberActualParameters > to THEN
  4728. Error(position, "more actual than formal parameters");
  4729. result := SyntaxTree.invalidExpression;
  4730. resultB := FALSE;
  4731. ELSE
  4732. resultB := TRUE;
  4733. END;
  4734. RETURN resultB
  4735. END CheckArity;
  4736. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4737. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4738. BEGIN
  4739. WHILE modifier # NIL DO
  4740. symbol := cellType.FindProperty(modifier.identifier);
  4741. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4742. propertyType := symbol.type.resolved;
  4743. modifierType := modifier.expression.type.resolved;
  4744. IF ~CompatibleTo(system, modifierType, propertyType) &
  4745. ~(
  4746. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4747. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4748. Error(modifier.position,"incompatible to cell property");
  4749. END;
  4750. ELSE
  4751. Error(modifier.position, "undefined property");
  4752. END;
  4753. modifier := modifier.nextModifier;
  4754. END;
  4755. END CheckModifiers;
  4756. BEGIN
  4757. type := NIL; result := NIL;
  4758. type0 := NIL; type1 := NIL; type2 := NIL;
  4759. numberActualParameters := actualParameters.Length();
  4760. IF numberActualParameters>0 THEN
  4761. parameter0 := actualParameters.GetExpression(0);
  4762. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4763. Error(parameter0.position,"forbidden type-less argument");
  4764. result := SyntaxTree.invalidExpression
  4765. END
  4766. END;
  4767. IF numberActualParameters >1 THEN
  4768. parameter1 := actualParameters.GetExpression(1);
  4769. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4770. ELSE
  4771. Error(parameter1.position,"forbidden type-less argument");
  4772. result := SyntaxTree.invalidExpression
  4773. END
  4774. END;
  4775. IF numberActualParameters >2 THEN
  4776. parameter2 := actualParameters.GetExpression(2);
  4777. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4778. ELSE
  4779. Error(parameter2.position,"forbidden type-less argument");
  4780. result := SyntaxTree.invalidExpression
  4781. END
  4782. END;
  4783. IF returnType # NIL THEN
  4784. id := Global.New;
  4785. result := NIL;
  4786. ELSE
  4787. id := builtin.id;
  4788. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4789. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4790. END;
  4791. END;
  4792. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4793. ELSIF result # NIL THEN type := result.type (* operator *)
  4794. ELSE
  4795. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4796. result(SyntaxTree.Designator).SetLeft(left);
  4797. IF returnType # NIL THEN
  4798. type := returnType;
  4799. END;
  4800. (* ---- ASSERT ----- *)
  4801. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4802. IF CheckBooleanType(parameter0) THEN
  4803. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4804. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4805. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4806. *)
  4807. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4808. Error(position, "static assertion failed");
  4809. END;
  4810. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4811. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4812. rules imposed by the architecture / current runtime
  4813. *)
  4814. END;
  4815. END;
  4816. (* ---- COPY ----- *)
  4817. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4818. IF~IsStringType(type0) THEN
  4819. Error(parameter0.position,"no string type");
  4820. END;
  4821. IF ~IsStringType(type1) THEN
  4822. Error(parameter1.position,"no string type");
  4823. ELSIF CheckVariable(parameter1) THEN
  4824. IF (type0 IS SyntaxTree.StringType) THEN
  4825. arrayType := type1(SyntaxTree.ArrayType);
  4826. IF arrayType.form = SyntaxTree.Static THEN
  4827. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4828. Error(position, "destination length smaller than source length")
  4829. END;
  4830. END;
  4831. END;
  4832. END;
  4833. (* ---- INC, DEC----- *)
  4834. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4835. IF numberActualParameters = 1 THEN
  4836. parameter1 :=Global.NewIntegerValue(system,position,1);
  4837. actualParameters.AddExpression(parameter1);
  4838. END;
  4839. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4840. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4841. Error(position, "incompatible increment");
  4842. ELSE
  4843. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4844. actualParameters.SetExpression(1,parameter1);
  4845. END;
  4846. END;
  4847. (* ---- EXCL, INCL----- *)
  4848. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4849. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4850. IF IsIntegerValue(parameter1,i0) THEN
  4851. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4852. Error(position, "parameter out of SET range")
  4853. END;
  4854. END;
  4855. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4856. actualParameters.SetExpression(1,parameter1);
  4857. END;
  4858. (* ---- HALT, SYSTEM.HALT ----- *)
  4859. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4860. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4861. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4862. rules imposed by the architecture / current runtime
  4863. *)
  4864. END;
  4865. (* ---- WAIT ----- *)
  4866. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4867. IF CheckObjectType(parameter0) THEN
  4868. END;
  4869. (* ---- NEW ----- *)
  4870. ELSIF (id = Global.New) THEN
  4871. IF returnType # NIL THEN
  4872. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4873. ELSE
  4874. first := 1;
  4875. END;
  4876. IF CheckArity(first,Infinity) THEN
  4877. IF currentIsRealtime THEN
  4878. Error(position, "forbidden new in realtime block");
  4879. END;
  4880. IF IsUnsafePointer(type0) THEN
  4881. Error(position, "forbidden new on unsafe pointer");
  4882. END;
  4883. (* check constructor *)
  4884. IF (first =0) OR CheckVariable(parameter0) THEN
  4885. IF type0 IS SyntaxTree.PointerType THEN
  4886. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4887. ELSIF type0 IS SyntaxTree.CellType THEN
  4888. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4889. ELSE
  4890. Error(position, "forbidden new on value type");
  4891. END;
  4892. IF type0 IS SyntaxTree.ArrayType THEN
  4893. arrayType := type0(SyntaxTree.ArrayType);
  4894. IF arrayType.form = SyntaxTree.Static THEN
  4895. i := first
  4896. ELSIF arrayType.form = SyntaxTree.Open THEN
  4897. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4898. ELSE HALT(100)
  4899. END;
  4900. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4901. i := first;
  4902. REPEAT
  4903. actualParameter := actualParameters.GetExpression(i);
  4904. IF CheckSizeType(actualParameter) THEN
  4905. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4906. actualParameters.SetExpression(i,actualParameter);
  4907. END;
  4908. INC(i);
  4909. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4910. END;
  4911. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4912. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4913. Error(position, "forbidden new on abstract object");
  4914. END;
  4915. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4916. IF constructor = NIL THEN
  4917. IF CheckArity(first,first) THEN END;
  4918. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4919. Error(position, "new on object with hidden constructor");
  4920. ELSE
  4921. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4922. numberFormalParameters := procedureType.numberParameters;
  4923. IF numberActualParameters-first <= numberFormalParameters THEN
  4924. formalParameter := procedureType.firstParameter;
  4925. FOR i := first TO numberActualParameters-1 DO
  4926. actualParameter := actualParameters.GetExpression(i);
  4927. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4928. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4929. ELSE
  4930. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4931. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4932. END;
  4933. actualParameters.SetExpression(i,actualParameter);
  4934. END;
  4935. formalParameter := formalParameter.nextParameter;
  4936. END;
  4937. WHILE (formalParameter # NIL) DO
  4938. IF formalParameter.defaultValue # NIL THEN
  4939. actualParameters.AddExpression(formalParameter.defaultValue);
  4940. formalParameter := formalParameter.nextParameter
  4941. ELSE
  4942. Error(position, "less actual than formal parameters");
  4943. formalParameter := NIL;
  4944. END;
  4945. END;
  4946. ELSE
  4947. Error(position, "more actual than formal parameters")
  4948. END;
  4949. END;
  4950. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4951. mathArrayType := type0(SyntaxTree.MathArrayType);
  4952. IF mathArrayType.form = SyntaxTree.Static THEN
  4953. Error(position, "new on static array");
  4954. ELSE
  4955. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4956. i0 := first+1; i1 := Infinity;
  4957. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4958. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4959. i1 := i0;
  4960. ELSE HALT(100);
  4961. END;
  4962. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4963. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4964. base := ArrayBase(type0,MAX(LONGINT));
  4965. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4966. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4967. IF ~CompatibleTo(system,type0,parameterType) THEN
  4968. Error(parameter0.position,"incompatible parameter in new");
  4969. result := SyntaxTree.invalidExpression;
  4970. ELSE
  4971. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4972. END;
  4973. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4974. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4975. IF ~CompatibleTo(system,type1,parameterType) THEN
  4976. Error(parameter1.position,"parameter incompatible to math array len type");
  4977. result := SyntaxTree.invalidExpression;
  4978. ELSE
  4979. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4980. END;
  4981. ELSE
  4982. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4983. i := first;
  4984. REPEAT
  4985. actualParameter := actualParameters.GetExpression(i);
  4986. IF CheckSizeType(actualParameter) THEN
  4987. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4988. actualParameters.SetExpression(i,actualParameter);
  4989. END;
  4990. INC(i);
  4991. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4992. END;
  4993. END;
  4994. END;
  4995. ELSIF type0 IS SyntaxTree.CellType THEN
  4996. IF ~(currentIsCellNet) THEN
  4997. Error(position, "cell allocation outside activeCells ");
  4998. ELSE
  4999. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  5000. IF (constructor = NIL) & CheckArity(1,1) THEN
  5001. (* ok *)
  5002. ELSE
  5003. procedureType := constructor.type(SyntaxTree.ProcedureType);
  5004. numberFormalParameters := procedureType.numberParameters;
  5005. DEC(numberActualParameters);
  5006. IF numberActualParameters <= numberFormalParameters THEN
  5007. formalParameter := procedureType.firstParameter;
  5008. FOR i := first TO numberActualParameters DO
  5009. actualParameter := actualParameters.GetExpression(i);
  5010. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  5011. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5012. ELSE
  5013. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  5014. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  5015. END;
  5016. actualParameters.SetExpression(i,actualParameter);
  5017. END;
  5018. formalParameter := formalParameter.nextParameter;
  5019. END;
  5020. WHILE (formalParameter # NIL) DO
  5021. IF formalParameter.defaultValue # NIL THEN
  5022. actualParameters.AddExpression(formalParameter.defaultValue);
  5023. formalParameter := formalParameter.nextParameter
  5024. ELSE
  5025. Error(position, "less actual than formal parameters");
  5026. formalParameter := NIL;
  5027. END;
  5028. END;
  5029. ELSE
  5030. Error(position, "more actual than formal parameters")
  5031. END;
  5032. END;
  5033. END;
  5034. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  5035. activeCellsStatement := TRUE;
  5036. ELSE
  5037. Error(position, "cannot be allocated");
  5038. END;
  5039. END;
  5040. END;
  5041. (* ---- DISPOSE ----- *)
  5042. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5043. IF ~IsPointerType(parameter0.type) THEN
  5044. Error(parameter0.position,"is not a pointer")
  5045. ELSIF ~IsDisposable(parameter0.type) THEN
  5046. Error(parameter0.position,"is not disposable")
  5047. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5048. END
  5049. (* ---- GETPROCEDURE ----- *)
  5050. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5051. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5052. IF CheckVariable(parameter2) THEN
  5053. IF ~GetProcedureAllowed(parameter2.type) THEN
  5054. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5055. END;
  5056. END;
  5057. END;
  5058. (* ---- ABS ----- *)
  5059. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5060. (* note: ABS on complex numbers is done using overloading *)
  5061. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5062. type := type0;
  5063. IF IsIntegerValue(parameter0,i0) THEN
  5064. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5065. type := Global.GetIntegerType(system,ABS(i0));
  5066. ELSIF IsRealValue(parameter0,r) THEN
  5067. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5068. END;
  5069. ELSE
  5070. type := SyntaxTree.invalidType;
  5071. END;
  5072. (* ---- ASH, ASR ----- *)
  5073. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5074. type := type0;
  5075. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5076. (*
  5077. ConvertOperands(parameter0,parameter1); (* same type *)
  5078. *)
  5079. type := parameter0.type;
  5080. IF IsIntegerValue(parameter0,i0) THEN
  5081. IF IsIntegerValue(parameter1,i1) THEN
  5082. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5083. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5084. result := ResolveExpression(result);
  5085. type := Global.GetIntegerType(system,i0);
  5086. END;
  5087. END;
  5088. IF type.resolved.sizeInBits < 32 THEN
  5089. type := system.longintType;
  5090. END;
  5091. (*!compatibility with release, remove when resolved
  5092. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5093. *)
  5094. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5095. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5096. actualParameters.SetExpression(0,parameter0);
  5097. actualParameters.SetExpression(1,parameter1);
  5098. END;
  5099. (* ---- CAP ----- *)
  5100. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5101. type := system.characterType;
  5102. IF CheckCharacterType (parameter0) THEN
  5103. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5104. actualParameters.SetExpression(0,parameter0);
  5105. IF IsCharacterValue(parameter0,c) THEN
  5106. IF (c <= "z") & (c >= "a") THEN
  5107. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5108. ELSE
  5109. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5110. END;
  5111. END;
  5112. END;
  5113. (* ---- CHR ----- *)
  5114. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5115. IF id = Global.Chr THEN
  5116. type := system.characterType
  5117. ELSE
  5118. type := system.characterType32
  5119. END;
  5120. IF CheckIntegerType(parameter0) THEN
  5121. IF IsIntegerValue(parameter0,i0) THEN
  5122. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5123. result := ResolveExpression(result);
  5124. ELSE
  5125. (*
  5126. result := NewConversion(parameter0.position,parameter0,type);
  5127. *)
  5128. END;
  5129. END
  5130. (* ---- ENTIER ----- *)
  5131. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5132. type := system.longintType;
  5133. IF CheckRealType(parameter0) THEN
  5134. IF IsRealValue(parameter0,r) THEN
  5135. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5136. type := Global.GetIntegerType(system,ENTIER(r));
  5137. END
  5138. END;
  5139. (* ---- ENTIERH ----- *)
  5140. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5141. type := system.hugeintType;
  5142. IF CheckRealType(parameter0) THEN
  5143. IF IsRealValue(parameter0,r) THEN
  5144. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5145. END
  5146. END;
  5147. (* ---- LEN ----- *)
  5148. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5149. type := system.lenType;
  5150. base := type0;
  5151. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5152. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5153. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5154. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5155. Error(position, "forbidden len on unsafe pointer");
  5156. END;
  5157. type0 := base;
  5158. ELSE
  5159. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5160. type0 := parameter0.type.resolved;
  5161. actualParameters.SetExpression(0,parameter0);
  5162. base := type0;
  5163. END;
  5164. END;
  5165. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5166. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5167. IF i1 < 0 THEN
  5168. Error(position, "invalid dimension");
  5169. base := SyntaxTree.invalidType;
  5170. ELSE
  5171. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5172. IF (base # NIL) & Indexable(base) THEN
  5173. ELSE
  5174. Error(position, "len on no array");
  5175. IF VerboseErrorMessage THEN
  5176. Printout.Info("base",base);
  5177. END;
  5178. base := SyntaxTree.invalidType;
  5179. END;
  5180. END;
  5181. IF numberActualParameters=2 THEN
  5182. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5183. actualParameters.SetExpression(1,parameter1);
  5184. ELSIF base IS SyntaxTree.MathArrayType THEN
  5185. Error(position, "missing dimension specification");
  5186. END;
  5187. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5188. IF base IS SyntaxTree.ArrayType THEN
  5189. arrayType := base(SyntaxTree.ArrayType);
  5190. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  5191. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5192. result := Global.NewIntegerValue(system,position,value);
  5193. type := result.type;(* arrayType.length.type;*)
  5194. ASSERT(type # NIL);
  5195. END;
  5196. ELSIF base IS SyntaxTree.MathArrayType THEN
  5197. mathArrayType := base(SyntaxTree.MathArrayType);
  5198. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  5199. result := Global.NewIntegerValue(system,position,value);
  5200. type := result.type;
  5201. (*
  5202. type := mathArrayType.length.type;
  5203. *)
  5204. ASSERT(type # NIL);
  5205. END;
  5206. END;
  5207. END;
  5208. ELSE
  5209. type := system.lenType;
  5210. END;
  5211. (* ---- FIRST ---- *)
  5212. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5213. type := system.lenType;
  5214. IF CheckRangeType(parameter0) THEN END;
  5215. result.SetAssignable(parameter0.assignable)
  5216. (* ---- LAST ---- *)
  5217. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5218. type := system.lenType;
  5219. IF CheckRangeType(parameter0) THEN END;
  5220. result.SetAssignable(parameter0.assignable)
  5221. (* ---- STEP ---- *)
  5222. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5223. type := system.lenType;
  5224. IF CheckRangeType(parameter0) THEN END;
  5225. result.SetAssignable(parameter0.assignable)
  5226. (* ---- RE ---- *)
  5227. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5228. IF CheckNumberType(parameter0) THEN
  5229. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5230. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5231. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5232. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5233. type := parameter0.type
  5234. ELSE
  5235. type := system.realType
  5236. END
  5237. END;
  5238. result.SetAssignable(parameter0.assignable)
  5239. (* ---- IM ---- *)
  5240. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5241. IF CheckNumberType(parameter0) THEN
  5242. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5243. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5244. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5245. ELSE
  5246. type := system.realType;
  5247. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5248. END
  5249. END;
  5250. result.SetAssignable(parameter0.assignable)
  5251. (* ---- MAX ----- *)
  5252. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5253. IF numberActualParameters = 1 THEN
  5254. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5255. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5256. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5257. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5258. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5259. *)
  5260. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5261. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5262. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5263. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5264. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5265. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5266. END;
  5267. ELSE
  5268. Error(parameter0.position,"is not a type symbol");
  5269. END
  5270. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5271. ConvertOperands(parameter0,parameter1);
  5272. actualParameters.SetExpression(0,parameter0);
  5273. actualParameters.SetExpression(1,parameter1);
  5274. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5275. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5276. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5277. END;
  5278. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5279. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5280. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5281. END;
  5282. END;
  5283. type := parameter0.type;
  5284. ELSE type := SyntaxTree.invalidType;
  5285. END;
  5286. (* ---- MIN ----- *)
  5287. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5288. IF numberActualParameters = 1 THEN
  5289. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5290. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5291. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5292. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5293. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5294. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5295. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5296. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5297. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5298. END;
  5299. ELSE
  5300. Error(parameter0.position,"is not a type symbol");
  5301. END
  5302. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5303. ConvertOperands(parameter0,parameter1);
  5304. actualParameters.SetExpression(0,parameter0);
  5305. actualParameters.SetExpression(1,parameter1);
  5306. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5307. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5308. ELSE result.SetResolved(parameter1.resolved)
  5309. END;
  5310. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5311. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5312. ELSE result.SetResolved(parameter1.resolved)
  5313. END;
  5314. END;
  5315. type := parameter0.type;
  5316. ELSE type := SyntaxTree.invalidType;
  5317. END;
  5318. (* ---- ODD ----- *)
  5319. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5320. type := system.booleanType;
  5321. IF CheckIntegerType(parameter0) THEN
  5322. IF IsIntegerValue(parameter0,i0) THEN
  5323. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5324. type := system.booleanType;
  5325. END;
  5326. END;
  5327. (* ---- ORD ----- *)
  5328. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5329. IF id = Global.Ord THEN
  5330. type := system.integerType;
  5331. ELSE
  5332. type := system.longintType;
  5333. END;
  5334. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5335. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5336. actualParameters.SetExpression(0,parameter0);
  5337. (* IF CheckCharacterType(parameter0) THEN*)
  5338. IF IsCharacterValue(parameter0,c)THEN
  5339. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5340. type := Global.GetSignedIntegerType(system,ORD(c));
  5341. END;
  5342. ELSE Error(parameter0.position, "incompatible parameter");
  5343. END;
  5344. (* ---- SHORT ----- *)
  5345. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5346. type := type0;
  5347. IF IsSignedIntegerType(type) THEN
  5348. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5349. ELSIF type = system.integerType THEN type := system.shortintType
  5350. ELSIF type = system.longintType THEN type := system.integerType
  5351. ELSIF type = system.hugeintType THEN type:= system.longintType
  5352. ELSE
  5353. CASE type.sizeInBits OF
  5354. 16: type := Global.Integer8
  5355. |32: type := Global.Integer16
  5356. |64: type := Global.Integer32
  5357. END;
  5358. END;
  5359. ELSIF IsUnsignedIntegerType(type) THEN
  5360. IF (type.sizeInBits = 8) OR (type = Global.Unsigned8) THEN Error(parameter0.position,"short not applicable")
  5361. ELSE
  5362. CASE type.sizeInBits OF
  5363. 16: type := Global.Unsigned8
  5364. |32: type := Global.Unsigned16
  5365. |64: type := Global.Unsigned32
  5366. END;
  5367. END;
  5368. ELSIF type IS SyntaxTree.FloatType THEN
  5369. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5370. ELSIF type = system.longrealType THEN type := system.realType
  5371. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5372. END;
  5373. ELSIF type IS SyntaxTree.ComplexType THEN
  5374. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5375. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5376. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5377. END;
  5378. ELSE
  5379. Error(parameter0.position,"short not applicable")
  5380. END;
  5381. IF (parameter0.resolved # NIL) THEN
  5382. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5383. IF parameter0 IS SyntaxTree.Value THEN
  5384. result.SetResolved(parameter0(SyntaxTree.Value));
  5385. END;
  5386. END;
  5387. (* ---- LONG ----- *)
  5388. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5389. type := type0;
  5390. IF IsSignedIntegerType(type) THEN
  5391. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5392. ELSIF type = system.longintType THEN type := system.hugeintType
  5393. ELSIF type = system.integerType THEN type := system.longintType
  5394. ELSIF type = system.shortintType THEN type := system.integerType
  5395. ELSE
  5396. CASE type.sizeInBits OF
  5397. 8: type := Global.Integer16
  5398. |16: type := Global.Integer32
  5399. |32: type := Global.Integer64
  5400. END;
  5401. END;
  5402. ELSIF IsUnsignedIntegerType(type) THEN
  5403. IF (type.sizeInBits = 64) OR (type = Global.Unsigned64) THEN Error(parameter0.position,"long not applicable")
  5404. ELSE
  5405. CASE type.sizeInBits OF
  5406. 8: type := Global.Unsigned16
  5407. |16: type := Global.Unsigned32
  5408. |32: type := Global.Unsigned64
  5409. END;
  5410. END;
  5411. ELSIF type IS SyntaxTree.FloatType THEN
  5412. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5413. ELSIF type= system.realType THEN type := system.longrealType
  5414. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5415. END;
  5416. ELSIF type IS SyntaxTree.ComplexType THEN
  5417. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5418. ELSIF type = system.complexType THEN type := system.longcomplexType
  5419. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5420. END;
  5421. ELSE
  5422. Error(parameter0.position,"long not applicable")
  5423. END;
  5424. IF (parameter0.resolved # NIL) THEN
  5425. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5426. IF parameter0 IS SyntaxTree.Value THEN
  5427. result.SetResolved(parameter0(SyntaxTree.Value));
  5428. END;
  5429. END;
  5430. (* ---- SIZE OF ----- *)
  5431. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5432. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5433. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5434. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5435. type := system.integerType;
  5436. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5437. ELSE
  5438. (* for variables, system sizeof could represent the physically occupied size
  5439. determined via the type descriptor, implement that ? *)
  5440. Error(parameter0.position,"is not a type symbol");
  5441. END
  5442. (* ---- SYSTEM.TRACE -----*)
  5443. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5444. FOR i := 0 TO numberActualParameters-1 DO
  5445. parameter0 := actualParameters.GetExpression(i);
  5446. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5447. Error(parameter0.position,"incompatible parameter");
  5448. END;
  5449. END;
  5450. (* remaining issues can only be tested in backend *)
  5451. (* ---- ADDRESSOF----- *)
  5452. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5453. IF HasAddress(parameter0) THEN
  5454. type := system.addressType;
  5455. ELSE
  5456. type := SyntaxTree.invalidType;
  5457. Error(parameter0.position,"has no address");
  5458. END;
  5459. (* ---- BIT ----- *)
  5460. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5461. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5462. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5463. actualParameters.SetExpression(0,parameter0);
  5464. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5465. actualParameters.SetExpression(1,parameter1);
  5466. END;
  5467. type := system.booleanType;
  5468. (* ----- MSK ---- *)
  5469. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5470. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5471. ConvertOperands(parameter0,parameter1);
  5472. actualParameters.SetExpression(0,parameter0);
  5473. actualParameters.SetExpression(1,parameter1);
  5474. END;
  5475. type := parameter0.type;
  5476. (* ---- SYSTEM.GET64 ----- *)
  5477. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5478. IF CheckAddressType(parameter0) THEN
  5479. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5480. actualParameters.SetExpression(0,parameter0);
  5481. END;
  5482. type := system.hugeintType;
  5483. (* ---- SYSTEM.GET32 ----- *)
  5484. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5485. IF CheckAddressType(parameter0) THEN
  5486. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5487. actualParameters.SetExpression(0,parameter0);
  5488. END;
  5489. type := system.longintType;
  5490. (* ---- SYSTEM.GET16 ----- *)
  5491. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5492. IF CheckAddressType(parameter0) THEN
  5493. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5494. actualParameters.SetExpression(0,parameter0);
  5495. END;
  5496. type := system.integerType;
  5497. (* ---- SYSTEM.GET8 ----- *)
  5498. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5499. IF CheckAddressType(parameter0) THEN
  5500. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5501. actualParameters.SetExpression(0,parameter0);
  5502. END;
  5503. type := system.shortintType;
  5504. (* ---- SYSTEM.GetStackPointer ----- *)
  5505. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5506. type := system.addressType;
  5507. (* ---- SYSTEM.GetFramePointer ----- *)
  5508. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5509. type := system.addressType;
  5510. (* ---- SYSTEM.GetActivity ----- *)
  5511. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5512. type := system.objectType;
  5513. (* ---- SYSTEM.SetStackPointer ----- *)
  5514. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5515. IF CheckAddressType(parameter0) THEN
  5516. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5517. actualParameters.SetExpression(0,parameter0);
  5518. END;
  5519. (* ---- SYSTEM.SetFramePointer ----- *)
  5520. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5521. IF CheckAddressType(parameter0) THEN
  5522. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5523. actualParameters.SetExpression(0,parameter0);
  5524. END;
  5525. (* ---- SYSTEM.SetActivity ----- *)
  5526. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5527. IF CheckObjectType(parameter0) THEN
  5528. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5529. actualParameters.SetExpression(0,parameter0);
  5530. END;
  5531. (* ---- LSH, LSL, ROT, ROR ----- *)
  5532. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5533. type := type0;
  5534. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5535. actualParameters.SetExpression(1, parameter1);
  5536. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5537. IF id = Global.Lsh THEN
  5538. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5539. ELSIF id = Global.Rot THEN
  5540. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5541. ELSIF id = Global.Ror THEN
  5542. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5543. END;
  5544. END;
  5545. (* ---- SYSTEM.VAL ----- *)
  5546. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5547. IF CheckTypeDeclarationType(parameter0) THEN
  5548. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5549. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5550. result := SyntaxTree.invalidExpression;
  5551. Error(parameter0.position,"is no basic type");
  5552. ELSE
  5553. IF (parameter1.resolved # NIL) THEN
  5554. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5555. IF parameter0 IS SyntaxTree.Value THEN
  5556. result.SetResolved(parameter0(SyntaxTree.Value));
  5557. END;
  5558. END;
  5559. result.SetAssignable(parameter1.assignable);
  5560. END;
  5561. END;
  5562. (* ---- SYSTEM.GET ----- *)
  5563. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5564. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5565. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5566. actualParameters.SetExpression(0,parameter0);
  5567. END;
  5568. (* ---- SYSTEM.PUT ----- *)
  5569. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5570. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5571. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5572. actualParameters.SetExpression(0,parameter0);
  5573. END;
  5574. (* ---- SYSTEM.PUT64 ----- *)
  5575. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5576. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5577. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5578. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5579. actualParameters.SetExpression(0,parameter0);
  5580. actualParameters.SetExpression(1,parameter1);
  5581. END;
  5582. (* ---- SYSTEM.PUT32 ----- *)
  5583. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5584. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5585. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5586. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5587. actualParameters.SetExpression(0,parameter0);
  5588. actualParameters.SetExpression(1,parameter1);
  5589. END;
  5590. (* ---- SYSTEM.PUT16 ----- *)
  5591. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5592. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5593. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5594. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5595. actualParameters.SetExpression(0,parameter0);
  5596. actualParameters.SetExpression(1,parameter1);
  5597. END;
  5598. (* ---- SYSTEM.PUT8 ----- *)
  5599. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5600. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5601. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5602. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5603. actualParameters.SetExpression(0,parameter0);
  5604. actualParameters.SetExpression(1,parameter1);
  5605. END;
  5606. (* ---- SYSTEM.MOVE ----- *)
  5607. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5608. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5609. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5610. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5611. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5612. actualParameters.SetExpression(0,parameter0);
  5613. actualParameters.SetExpression(1,parameter1);
  5614. actualParameters.SetExpression(2,parameter2);
  5615. END;
  5616. (* ---- SYSTEM.NEW ----- *)
  5617. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5618. IF ~IsPointerType(parameter0.type) THEN
  5619. Error(parameter0.position,"is not a pointer")
  5620. ELSIF CheckSizeType(parameter1) THEN
  5621. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5622. actualParameters.SetExpression(1,parameter1);
  5623. END;
  5624. (* ----SYSTEM.REF ---- *)
  5625. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5626. type := system.addressType
  5627. (* ---- INCR ----- *)
  5628. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5629. type := system.lenType;
  5630. base := type0;
  5631. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5632. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5633. IF i1 < 0 THEN
  5634. Error(position, "invalid dimension");
  5635. base := SyntaxTree.invalidType;
  5636. ELSE
  5637. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5638. IF (base # NIL) & Indexable(base) THEN
  5639. ELSE
  5640. Error(position, "len on no array");
  5641. IF VerboseErrorMessage THEN
  5642. Printout.Info("base",base);
  5643. END;
  5644. base := SyntaxTree.invalidType;
  5645. END;
  5646. END;
  5647. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5648. actualParameters.SetExpression(1,parameter1);
  5649. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5650. mathArrayType := base(SyntaxTree.MathArrayType);
  5651. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5652. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5653. type := system.lenType;
  5654. END;
  5655. END;
  5656. ELSE
  5657. type := system.lenType;
  5658. END;
  5659. (* ---- SUM ----- *)
  5660. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5661. Error(position, "sum operator not applicable");
  5662. (* ---- ALL ----- *)
  5663. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5664. Error(position, "all operator not applicable");
  5665. (* ---- DIM ----- *)
  5666. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5667. type := system.lenType;
  5668. IF type0 IS SyntaxTree.MathArrayType THEN
  5669. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5670. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5671. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5672. END;
  5673. ELSE
  5674. Error(position, "dimension on non math array type");
  5675. END;
  5676. (* ---- CAS ----- *)
  5677. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5678. IF type0.IsComposite () THEN
  5679. Error(position, "first parameter of composite type");
  5680. result := SyntaxTree.invalidExpression;
  5681. ELSIF ~IsVariable (parameter0) THEN
  5682. Error(position, "first parameter not assignable");
  5683. result := SyntaxTree.invalidExpression;
  5684. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5685. Error(position, "second parameter incompatible");
  5686. result := SyntaxTree.invalidExpression;
  5687. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5688. Error(position, "third parameter incompatible");
  5689. result := SyntaxTree.invalidExpression;
  5690. ELSE
  5691. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5692. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5693. type := type0;
  5694. END;
  5695. (* ---- RESHAPE ----- *)
  5696. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5697. IF type0 IS SyntaxTree.MathArrayType THEN
  5698. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5699. base := ArrayBase(type0,MAX(LONGINT));
  5700. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5701. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5702. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5703. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5704. IF ~CompatibleTo(system,type0,parameterType) THEN
  5705. Error(parameter0.position,"incompatible parameter in reshape");
  5706. result := SyntaxTree.invalidExpression;
  5707. ELSE
  5708. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5709. END;
  5710. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5711. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  5712. IF ~CompatibleTo(system,type1,parameterType) THEN
  5713. Error(parameter1.position,"parameter incompatible to math array of longint");
  5714. result := SyntaxTree.invalidExpression;
  5715. ELSE
  5716. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5717. END;
  5718. ELSE
  5719. Error(position,"reshape on non math array type");
  5720. result := SyntaxTree.invalidExpression;
  5721. END;
  5722. (* ---- SYSTEM.TYPECODE ----- *)
  5723. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5724. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5725. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5726. type := type.resolved;
  5727. IF type IS SyntaxTree.PointerType THEN
  5728. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5729. END;
  5730. IF ~(type IS SyntaxTree.RecordType) THEN
  5731. Error(parameter0.position,"must be type with type descriptor");
  5732. END;
  5733. ELSE
  5734. Error(parameter0.position,"is not a type symbol");
  5735. END;
  5736. type := system.addressType;
  5737. (* -------- FLT --------- *)
  5738. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5739. type := system.realType;
  5740. IF IsRealValue(parameter0, r) THEN
  5741. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5742. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  5743. value := ABS(i0);
  5744. IF value # 0 THEN
  5745. i1 := 23;
  5746. IF value >= 2*800000H THEN
  5747. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  5748. ELSIF value < 800000H THEN
  5749. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  5750. END;
  5751. value := (i1 + 127)*800000H - 800000H + value;
  5752. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  5753. END;
  5754. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value)));
  5755. END;
  5756. (* ------- CONNECT -------*)
  5757. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5758. (*IF ~(currentIsCellNet) THEN
  5759. Error(position, "connection outside activeCells body block");
  5760. END;*)
  5761. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5762. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5763. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5764. END;
  5765. IF numberActualParameters = 3 THEN
  5766. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5767. Error(position, "incompatible channel size parameter");
  5768. END;
  5769. *)
  5770. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5771. actualParameters.SetExpression(2,parameter2);
  5772. END;
  5773. activeCellsStatement := TRUE;
  5774. (* ---------- DELEGATE --------*)
  5775. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5776. (*
  5777. IF ~(currentIsCellNet) THEN
  5778. Error(position, "connection delegation outside activeCells body block");
  5779. END;
  5780. *)
  5781. IF ~CheckPortType(parameter1, inPort) THEN
  5782. Error(parameter0.position,"not a port")
  5783. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5784. Error(parameter1.position,"not a port")
  5785. ELSIF (outPort.direction # inPort.direction) THEN
  5786. Error(parameter0.position,"invalid port direction");
  5787. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5788. Error(position, "incompatible port sizes");
  5789. END;
  5790. activeCellsStatement := TRUE;
  5791. (* --------- RECEIVE ---------*)
  5792. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5793. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5794. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5795. IF inPort.direction # SyntaxTree.InPort THEN
  5796. Error(parameter0.position,"not an in-port")
  5797. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5798. Error(parameter1.position,"incompatible to port type");
  5799. END;
  5800. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5801. IF ~SameType(parameter2.type, system.integerType) THEN
  5802. Error(parameter2.position,"incompatible to integer type");
  5803. END;
  5804. END;
  5805. END;
  5806. (* --------- SEND ---------*)
  5807. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5808. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5809. IF CheckPortType(parameter0,outPort) THEN
  5810. IF outPort.direction # SyntaxTree.OutPort THEN
  5811. Error(parameter1.position,"not an out-port")
  5812. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5813. Error(parameter1.position,"incompatible to port type");
  5814. ELSE
  5815. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5816. actualParameters.SetExpression(1,parameter1);
  5817. END;
  5818. END;
  5819. (* ------- custom builtins ----- *)
  5820. ELSIF id = Global.systemSpecial THEN
  5821. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5822. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5823. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5824. type := procedureType.returnType;
  5825. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5826. (* go through all formal parameters *)
  5827. formalParameter := procedureType.firstParameter;
  5828. FOR i := 0 TO actualParameters.Length() - 1 DO
  5829. actualParameter := actualParameters.GetExpression(i);
  5830. IF actualParameter = SyntaxTree.invalidExpression THEN
  5831. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5832. Error(position, "incompatible parameter")
  5833. ELSE
  5834. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5835. END;
  5836. actualParameters.SetExpression(i, actualParameter);
  5837. formalParameter := formalParameter.nextParameter
  5838. END
  5839. END
  5840. ELSE
  5841. Error(position, "builtin not implemented");
  5842. result := SyntaxTree.invalidExpression;
  5843. END;
  5844. END;
  5845. IF result # SyntaxTree.invalidExpression THEN
  5846. type := ResolveType(type);
  5847. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5848. result.SetType(type);
  5849. END;
  5850. RETURN result
  5851. END NewBuiltinCallDesignator;
  5852. (** return type guard designator left(type)
  5853. - check if type can be extended (i.e. is no static record)
  5854. - check if type is a type extension of left.type
  5855. - returns new type guard designator
  5856. returns invalidDesignator = invalidExpression if error
  5857. **)
  5858. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5859. VAR result: SyntaxTree.Designator;
  5860. BEGIN
  5861. result := SyntaxTree.invalidDesignator;
  5862. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5863. Error(position, "no type extension of type");
  5864. IF VerboseErrorMessage THEN
  5865. Printout.Info("left",left);
  5866. Printout.Info("type",type);
  5867. END;
  5868. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5869. Error(position, "variable cannot be extended");
  5870. ELSIF IsUnsafePointer(left.type) THEN
  5871. Error(position, "forbidden type guard on unsafe pointer");
  5872. ELSE
  5873. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5874. result.SetType(type);
  5875. result.SetAssignable(left.assignable);
  5876. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5877. END;
  5878. RETURN result
  5879. END NewTypeGuardDesignator;
  5880. (** check and resolve parameter designator left(expression list)
  5881. - check expression list
  5882. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5883. - elsif left is a procedure type then
  5884. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5885. - else return is a procedure call then return ProcedureCallDesignator
  5886. returns invalidDesignator = invalidExpression if error
  5887. **)
  5888. PROCEDURE VisitParameterDesignator*(designator: SyntaxTree.ParameterDesignator);
  5889. VAR
  5890. parameters: SyntaxTree.ExpressionList;
  5891. left: SyntaxTree.Designator;
  5892. result,expression: SyntaxTree.Expression;
  5893. typeDeclaration: SyntaxTree.TypeDeclaration;
  5894. type, expressionType: SyntaxTree.Type;
  5895. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5896. BEGIN
  5897. type := type.resolved;
  5898. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5899. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5900. END;
  5901. RETURN type
  5902. END BaseType;
  5903. BEGIN
  5904. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5905. result := SyntaxTree.invalidDesignator;
  5906. left := ResolveDesignator(designator.left);
  5907. IF left # SyntaxTree.invalidDesignator THEN
  5908. parameters := designator.parameters;
  5909. IF ExpressionList(parameters) THEN
  5910. IF (left.type = NIL) THEN
  5911. Error(left.position,"object is not a procedure or cannot be extended");
  5912. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5913. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5914. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5915. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5916. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5917. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5918. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5919. ELSE
  5920. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5921. END
  5922. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5923. expression := parameters.GetExpression(0);
  5924. type := typeDeclaration.declaredType.resolved;
  5925. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5926. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5927. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5928. OR (expressionType IS SyntaxTree.EnumerationType)
  5929. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5930. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5931. Error(left.position,"invalid unsigned type in explicit conversion");
  5932. ELSE
  5933. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5934. END;
  5935. ELSE
  5936. Error(left.position,"invalid type in explicit conversion");
  5937. END;
  5938. ELSE
  5939. Error(left.position,"called object is not a procedure or cannot be extended");
  5940. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5941. result := SyntaxTree.invalidDesignator;
  5942. END;
  5943. ELSE
  5944. result := SyntaxTree.invalidDesignator
  5945. END;
  5946. END;
  5947. resolvedExpression := result;
  5948. END VisitParameterDesignator;
  5949. (** check dereference designator left^
  5950. - check if left is pointer type or left is object type
  5951. - return new dereference designator with type = left.baseType.type (if appropriate)
  5952. with error handling
  5953. returns invalidDesignator = invalidExpression if error
  5954. **)
  5955. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5956. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5957. BEGIN
  5958. result := SyntaxTree.invalidDesignator;
  5959. type := left.type;
  5960. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5961. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5962. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5963. result.SetAssignable(TRUE);
  5964. result.SetType(type);
  5965. result.SetHidden(left.isHidden);
  5966. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5967. type := type.resolved;
  5968. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5969. result.SetAssignable(TRUE);
  5970. result.SetType(type);
  5971. result.SetHidden(left.isHidden);
  5972. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5973. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5974. result.SetAssignable(TRUE);
  5975. result.SetType(type);
  5976. result.SetHidden(left.isHidden);
  5977. ELSE
  5978. Error(position, "dereference on no pointer");
  5979. IF VerboseErrorMessage THEN
  5980. Printout.Info("pointer", type);
  5981. Printout.Info("scope", currentScope);
  5982. END;
  5983. END;
  5984. RETURN result
  5985. END NewDereferenceDesignator;
  5986. (** check supercall designator left^
  5987. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5988. - return new supercall designator with type = left.type
  5989. with error handling
  5990. **)
  5991. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5992. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5993. objectScope: SyntaxTree.Scope;
  5994. BEGIN
  5995. result := SyntaxTree.invalidDesignator;
  5996. IF left = SyntaxTree.invalidDesignator THEN
  5997. (* error already handled *)
  5998. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5999. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  6000. ASSERT(symbol # SyntaxTree.invalidSymbol);
  6001. IF symbol IS SyntaxTree.Procedure THEN
  6002. procedure := symbol(SyntaxTree.Procedure);
  6003. objectScope := currentScope;
  6004. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  6005. objectScope := objectScope.outerScope;
  6006. END;
  6007. IF (left.left = NIL) OR ~
  6008. (
  6009. (left.left IS SyntaxTree.SelfDesignator) OR
  6010. (left.left IS SyntaxTree.DereferenceDesignator)
  6011. & (left.left(SyntaxTree.Designator).left # NIL)
  6012. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  6013. Error(position, "procedure not in immediate object scope");
  6014. IF VerboseErrorMessage THEN
  6015. Printout.Info("left.left",left.left);
  6016. END;
  6017. ELSIF procedure.super # NIL THEN
  6018. result := SyntaxTree.NewSupercallDesignator(position,left);
  6019. result.SetType(left.type.resolved)
  6020. ELSE
  6021. Error(position, "no supermethod for this procedure");
  6022. END;
  6023. ELSE
  6024. Error(position, "symbol is not a procedure");
  6025. END;
  6026. ELSE
  6027. Error(position, "is no symbol designator");
  6028. END;
  6029. RETURN result
  6030. END NewSupercallDesignator;
  6031. (** check and semantically resolve arrow designator left^
  6032. - if left is procedure type -> result := SupercallDesignator
  6033. - else result := DereferenceDesignator
  6034. returns result via global variable resolvedExpression
  6035. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  6036. **)
  6037. PROCEDURE VisitArrowDesignator*(arrowDesignator: SyntaxTree.ArrowDesignator);
  6038. VAR left: SyntaxTree.Designator;
  6039. BEGIN
  6040. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  6041. left := ResolveDesignator(arrowDesignator.left);
  6042. IF left # NIL THEN
  6043. IF (left.type = NIL) THEN
  6044. Error(arrowDesignator.position,"Invalid arrow designator");
  6045. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  6046. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  6047. ELSE
  6048. IF IsPointerToObject(left.type) THEN
  6049. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  6050. END;
  6051. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6052. END
  6053. END
  6054. END VisitArrowDesignator;
  6055. (** check and return expression
  6056. - if expression has no type then resolve expression
  6057. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6058. - return result
  6059. **)
  6060. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6061. VAR result,prev: SyntaxTree.Expression;
  6062. BEGIN
  6063. IF expression = NIL THEN result := NIL
  6064. ELSIF (expression.type = NIL) THEN
  6065. prev := resolvedExpression;
  6066. resolvedExpression := SyntaxTree.invalidExpression;
  6067. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6068. expression.SetType(SyntaxTree.invalidType);
  6069. END;
  6070. VExpression(expression);
  6071. result := resolvedExpression;
  6072. IF currentIsRealtime THEN
  6073. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6074. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6075. END;
  6076. END;
  6077. (* designator modifiers for backends if they support it ...*)
  6078. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6079. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6080. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6081. END;
  6082. resolvedExpression := prev
  6083. ELSE
  6084. result := expression
  6085. END;
  6086. RETURN result
  6087. END ResolveExpression;
  6088. (**
  6089. check expression to be constant expression
  6090. - resolve expression
  6091. - if valid then check that of value type
  6092. report error and return invalidExpression if anything fails
  6093. **)
  6094. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6095. VAR position: Position;
  6096. BEGIN
  6097. position := expression.position;
  6098. expression := ResolveExpression(expression);
  6099. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6100. ELSIF (expression.resolved = NIL) THEN
  6101. Error(position, "expression is not constant");
  6102. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6103. expression := SyntaxTree.invalidExpression;
  6104. END;
  6105. RETURN expression
  6106. END ConstantExpression;
  6107. (** check expression to be constant integer
  6108. - resolve expresssion
  6109. - if valid then check that of integer value type
  6110. report error and return invalidExpression if anything fails
  6111. **)
  6112. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6113. VAR position: Position;
  6114. BEGIN
  6115. position := expression.position;
  6116. expression := ResolveExpression(expression);
  6117. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6118. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6119. expression := SyntaxTree.invalidExpression;
  6120. Error(position, "expression is not a constant integer");
  6121. END;
  6122. RETURN expression
  6123. END ConstantInteger;
  6124. (** check expression as positive (>=0) constant integer
  6125. - resolve expression
  6126. - if valid then check that integer value
  6127. - if integer value then check that value >= 0
  6128. report error and return invalidExpression if anything fails
  6129. **)
  6130. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6131. VAR position: Position;
  6132. BEGIN
  6133. position := expression.position;
  6134. expression := ConstantExpression(expression);
  6135. IF expression = SyntaxTree.invalidExpression THEN
  6136. (* error already reported *)
  6137. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6138. Error(position, "expression is not integer valued");
  6139. expression := SyntaxTree.invalidExpression
  6140. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value <0) THEN
  6141. Error(position, "integer is not greater or equal zero");
  6142. END;
  6143. RETURN expression
  6144. END ConstantIntegerGeq0;
  6145. (** check expression as condition
  6146. - resolve expression
  6147. - if valid expression then check that result type is boolean
  6148. report error and return invalidExpression if anything fails
  6149. **)
  6150. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6151. VAR position: Position;
  6152. BEGIN
  6153. position := expression.position;
  6154. expression := ResolveExpression(expression);
  6155. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6156. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6157. expression := SyntaxTree.invalidExpression;
  6158. Error(position, "expression is not boolean");
  6159. END;
  6160. RETURN expression
  6161. END ResolveCondition;
  6162. (*** symbols ***)
  6163. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6164. BEGIN
  6165. VSymbol(x);
  6166. END ResolveSymbol;
  6167. (** check a symbol
  6168. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6169. **)
  6170. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6171. VAR scope: SyntaxTree.Scope;
  6172. BEGIN
  6173. (* visibility *)
  6174. scope := symbol.scope;
  6175. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6176. scope := scope.outerScope;
  6177. END;
  6178. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6179. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6180. Error(symbol.position,"cannot be exported");
  6181. IF VerboseErrorMessage THEN
  6182. Printout.Info("symbol",symbol);
  6183. END;
  6184. END;
  6185. END;
  6186. END CheckSymbolVisibility;
  6187. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6188. If node is currently being resolved then emit a cyclic definition error.
  6189. Return TRUE only if node is fully resolved.
  6190. **)
  6191. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6192. VAR result: BOOLEAN;
  6193. BEGIN
  6194. IF SyntaxTree.Resolved IN x.state THEN
  6195. result := FALSE
  6196. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6197. Error(x.position,"cyclic definition");
  6198. result := FALSE;
  6199. ELSE
  6200. result := TRUE;
  6201. x.SetState(SyntaxTree.BeingResolved)
  6202. END;
  6203. RETURN result
  6204. END SymbolNeedsResolution;
  6205. (** check and resolve a type declaration symbol = Type
  6206. - set type to declaration type
  6207. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6208. This is so because the type declaration itself does not have a type but it only stands for a type.
  6209. In the implementation of the compiler this made a lot much easier.
  6210. - resolve and set declared type
  6211. - check symbol
  6212. **)
  6213. PROCEDURE VisitTypeDeclaration*(typeDeclaration: SyntaxTree.TypeDeclaration);
  6214. VAR prevScope: SyntaxTree.Scope;
  6215. BEGIN
  6216. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6217. IF SymbolNeedsResolution(typeDeclaration) THEN
  6218. typeDeclaration.SetState(SyntaxTree.Resolved);
  6219. prevScope := currentScope;
  6220. currentScope := typeDeclaration.scope;
  6221. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6222. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6223. CheckSymbolVisibility(typeDeclaration);
  6224. typeDeclaration.SetState(SyntaxTree.Resolved);
  6225. currentScope := prevScope;
  6226. END;
  6227. END VisitTypeDeclaration;
  6228. (** check and resolve a constant declaration symbol = (constant) expression
  6229. - check expression
  6230. - set type and value
  6231. - check symbol
  6232. **)
  6233. PROCEDURE VisitConstant*(constant: SyntaxTree.Constant);
  6234. VAR
  6235. expression: SyntaxTree.Expression;
  6236. type: SyntaxTree.Type;
  6237. name: Basic.SegmentedName;
  6238. replacement: Replacement;
  6239. BEGIN
  6240. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6241. IF SymbolNeedsResolution(constant) THEN
  6242. expression := constant.value;
  6243. IF replacements # NIL THEN
  6244. Global.GetSymbolSegmentedName(constant, name);
  6245. replacement := replacements;
  6246. WHILE (replacement # NIL) & (replacement.name # name) DO
  6247. replacement := replacement.next;
  6248. END;
  6249. IF replacement # NIL THEN
  6250. InfoSS(constant.position, "replacing constant", constant.name);
  6251. (*
  6252. NEW(stringReader, Strings.Length(replacement.string^));
  6253. stringReader.Set(replacement.string^);
  6254. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6255. NEW(parser, scanner, diagnostics);
  6256. expression := parser.Expression();
  6257. *)
  6258. expression := replacement.expression;
  6259. replacement.used := TRUE;
  6260. END;
  6261. END;
  6262. constant.SetType(SyntaxTree.invalidType);
  6263. expression := ConstantExpression(expression);
  6264. ASSERT(expression.type # NIL);
  6265. type := expression.type.resolved;
  6266. constant.SetType(type);
  6267. constant.SetValue(expression);
  6268. CheckSymbolVisibility(constant);
  6269. constant.SetState(SyntaxTree.Resolved);
  6270. END;
  6271. END VisitConstant;
  6272. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6273. VAR procedureAlignment: LONGINT;
  6274. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6275. (* least common multiple *)
  6276. VAR a,b: LONGINT;
  6277. BEGIN
  6278. a := a0; b := b0;
  6279. WHILE (a # b) DO
  6280. IF a < b THEN a := a+a0
  6281. ELSE b := b + b0
  6282. END;
  6283. END;
  6284. RETURN a
  6285. END LCM;
  6286. BEGIN
  6287. IF alignment > 1 THEN
  6288. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6289. IF (procedureAlignment > 1) THEN
  6290. alignment := LCM(alignment, procedureAlignment);
  6291. END;
  6292. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6293. END;
  6294. END AdaptStackAlignment;
  6295. (** check and resolve a variable / field
  6296. - check and set type
  6297. - negative check on open array type
  6298. - check symbol
  6299. **)
  6300. PROCEDURE VisitVariable*(variable: SyntaxTree.Variable);
  6301. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position; pointerType: SyntaxTree.PointerType;
  6302. BEGIN
  6303. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6304. IF SymbolNeedsResolution(variable) THEN
  6305. modifiers := variable.modifiers;
  6306. (*
  6307. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6308. variable.AddFlags(flags);
  6309. *)
  6310. variable.SetType(ResolveType(variable.type));
  6311. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6312. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6313. Error(variable.position,"forbidden open array variable");
  6314. END;
  6315. END;
  6316. CheckSymbolVisibility(variable);
  6317. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6318. variable.SetUntraced(TRUE);
  6319. IF ~ContainsPointer(variable.type) THEN
  6320. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6321. Error(position, "untraced flag on non-pointer variable");
  6322. END;
  6323. END;
  6324. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6325. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6326. IF ~PowerOf2(value) THEN
  6327. Error(position, "forbidden alignment - must be power of two");
  6328. ELSE
  6329. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6330. END;
  6331. END;
  6332. variable.SetAlignment(FALSE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6333. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6334. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6335. Error(position, "fixed position not possible in procedure");
  6336. END;
  6337. variable.SetAlignment(TRUE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6338. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6339. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6340. Error(position,"fictive offset not possible in procedure");
  6341. END;
  6342. variable.SetFictive(LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6343. variable.SetOffset(LONGINT(value)*system.dataUnit); (* TODO: fix explicit integer truncation *)
  6344. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6345. END;
  6346. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6347. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6348. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6349. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6350. END;
  6351. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6352. IF variable.initializer # NIL THEN
  6353. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6354. END;
  6355. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6356. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6357. pointerType.SetPointerBase(variable.type);
  6358. pointerType.SetHidden(TRUE);
  6359. variable.SetType(ResolveType(pointerType));
  6360. END;
  6361. variable.SetState(SyntaxTree.Resolved);
  6362. END;
  6363. END VisitVariable;
  6364. PROCEDURE VisitProperty*(property: SyntaxTree.Property);
  6365. BEGIN
  6366. VisitVariable(property)
  6367. END VisitProperty;
  6368. (** check and resolve a (procedure) parameter
  6369. - check and set type
  6370. - check symbol
  6371. - check parameter kind and set read-only flags if appropriate
  6372. **)
  6373. PROCEDURE VisitParameter*(parameter: SyntaxTree.Parameter);
  6374. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6375. BEGIN
  6376. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6377. IF SymbolNeedsResolution(parameter) THEN
  6378. modifiers := parameter.modifiers;
  6379. parameter.SetType(ResolveType(parameter.type));
  6380. ASSERT(parameter.type.resolved # NIL);
  6381. CheckSymbolVisibility(parameter);
  6382. IF parameter.defaultValue # NIL THEN
  6383. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6384. Error(parameter.position,"forbidden default value on non-value parameter");
  6385. ELSE
  6386. expression := ConstantExpression(parameter.defaultValue);
  6387. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6388. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6389. parameter.SetDefaultValue(expression);
  6390. END;
  6391. END;
  6392. END;
  6393. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6394. Error(parameter.position, "forbidden value parameter of math array type ");
  6395. END;
  6396. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6397. parameter.SetUntraced(TRUE);
  6398. IF ~ContainsPointer(parameter.type) THEN
  6399. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6400. Error(position, "untraced flag on non-pointer variable");
  6401. END;
  6402. END;
  6403. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6404. parameter.SetMoveable(TRUE);
  6405. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6406. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6407. Error(position, "illegal movable flag on non-address variable");
  6408. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6409. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6410. Error(position, "unnecessary movable flag on variable variable");
  6411. END;
  6412. END;
  6413. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6414. parameter.SetState(SyntaxTree.Resolved);
  6415. END;
  6416. END VisitParameter;
  6417. (** check and resolve a procedure (with declaration and implementation scope)
  6418. - check the procedure type
  6419. - check if method (i.e. in record scope), if so then
  6420. - check if (unique) constructor
  6421. - check if (unique) finalizer
  6422. - check if super method available, if so then check signature
  6423. - of not in record scope then negative check on constructor flag
  6424. - of not in record scope then negative check on finalizer flag
  6425. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6426. - check procedure symbol
  6427. **)
  6428. PROCEDURE VisitProcedure*(procedure: SyntaxTree.Procedure);
  6429. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6430. procedureType: SyntaxTree.ProcedureType;
  6431. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6432. qualifiedType: SyntaxTree.QualifiedType;
  6433. value: Basic.Integer;
  6434. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6435. position: Position;
  6436. fp: SyntaxTree.FingerPrint;
  6437. BEGIN
  6438. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6439. IF IsOberonInline(procedure) THEN
  6440. IF SyntaxTree.Public * procedure.access # {} THEN
  6441. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6442. END;
  6443. procedure.SetInline(FALSE);
  6444. procedure.SetOberonInline(TRUE);
  6445. END;
  6446. IF SymbolNeedsResolution(procedure) THEN
  6447. recentIsRealtime := currentIsRealtime;
  6448. recentIsBodyProcedure := currentIsBodyProcedure;
  6449. CheckSymbolVisibility(procedure);
  6450. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6451. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6452. modifiers := procedureType.modifiers;
  6453. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN
  6454. procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6455. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6456. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6457. END;
  6458. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6459. procedureType.SetInterrupt(TRUE);
  6460. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6461. END;
  6462. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6463. procedureType.SetNoReturn(TRUE);
  6464. END;
  6465. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  6466. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6467. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6468. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6469. END;
  6470. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6471. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6472. END;
  6473. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6474. IF ~PowerOf2(value) THEN
  6475. Error(position, "forbidden stack alignment - must be power of two");
  6476. ELSE
  6477. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6478. END;
  6479. END;
  6480. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6481. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6482. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6483. END;
  6484. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6485. SyntaxTree.InitFingerPrint(fp);
  6486. fp.shallow := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6487. fp.public := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6488. fp.private := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6489. fp.shallowAvailable := TRUE;
  6490. procedure.SetFingerPrint(fp);
  6491. END;
  6492. CheckModifiers(modifiers, TRUE);
  6493. modifiers := procedureType.returnTypeModifiers;
  6494. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6495. CheckModifiers(modifiers, TRUE);
  6496. procedure.SetState(SyntaxTree.Resolved);
  6497. FixProcedureType(procedureType);
  6498. currentIsRealtime := procedureType.isRealtime;
  6499. currentIsBodyProcedure := procedure.isBodyProcedure;
  6500. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6501. THEN
  6502. Error(procedure.position,"problems during parameter offset computation");
  6503. END;
  6504. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6505. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6506. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6507. Error(procedure.position,"problem during parameter offset generation");
  6508. END;
  6509. END;
  6510. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6511. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6512. procedureType.SetDelegate(TRUE);
  6513. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6514. (* add auto-self *)
  6515. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6516. IF (record.pointerType.typeDeclaration = NIL) THEN
  6517. selfParameter.SetType(record.pointerType);
  6518. ELSE
  6519. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6520. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6521. qualifiedType.SetResolved(record.pointerType);
  6522. selfParameter.SetType(qualifiedType);
  6523. END;
  6524. selfParameter.SetAccess(SyntaxTree.Hidden);
  6525. END;
  6526. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6527. IF procedure.isConstructor THEN
  6528. (*! constructor is always visible, compatibility to paco
  6529. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6530. *)
  6531. procedure.MarkUsed;
  6532. IF procedureType.returnType # NIL THEN
  6533. Error(procedure.position,"constructor with forbidden return type");
  6534. END;
  6535. proc := procedure.scope.firstProcedure;
  6536. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6537. proc := proc.nextProcedure;
  6538. END;
  6539. IF proc # NIL THEN
  6540. Error(procedure.position,"duplicate constructor")
  6541. ELSE
  6542. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6543. END;
  6544. END;
  6545. IF procedure.isFinalizer THEN
  6546. procedure.MarkUsed;
  6547. IF procedureType.returnType # NIL THEN
  6548. Error(procedure.position,"finalizer with forbidden return type");
  6549. END;
  6550. IF procedureType.numberParameters # 0 THEN
  6551. Error(procedure.position,"finalizer with formal parameters");
  6552. END;
  6553. proc := procedure.scope.firstProcedure;
  6554. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6555. proc := proc.nextProcedure;
  6556. END;
  6557. IF proc # NIL THEN
  6558. Error(procedure.position,"duplicate finalizer")
  6559. ELSE
  6560. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6561. END;
  6562. END;
  6563. super := FindSuperProcedure(record.recordScope, procedure);
  6564. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6565. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6566. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6567. END;
  6568. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6569. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6570. END;
  6571. IF super.isFinal THEN
  6572. Error(procedure.position,"forbidden method extending final method");
  6573. END;
  6574. (*
  6575. IF super.access # procedure.access THEN
  6576. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6577. END;
  6578. *)
  6579. procedure.SetSuper(super);
  6580. super.SetOverwritten(TRUE);
  6581. procedure.SetAccess(procedure.access+super.access);
  6582. procedure.MarkUsed;
  6583. END;
  6584. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6585. THEN
  6586. Error(procedure.position,"problems during parameter offset computation");
  6587. END;
  6588. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6589. IF cellsAreObjects THEN
  6590. procedureType.SetDelegate(TRUE);
  6591. END;
  6592. IF procedure.isConstructor THEN
  6593. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6594. END;
  6595. ELSIF procedure.isConstructor THEN
  6596. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6597. END;
  6598. Declarations(procedure.procedureScope, FALSE, {0,1});
  6599. (* body resolution part done as late fix of the procedure type *)
  6600. procedure.SetState(SyntaxTree.Resolved);
  6601. currentIsRealtime := recentIsRealtime;
  6602. currentIsBodyProcedure := recentIsBodyProcedure;
  6603. END;
  6604. END VisitProcedure;
  6605. (**
  6606. a builtin procedure is a global item that may not be modified locally
  6607. instead the resolving of builtin procedure calls are done in the esignator
  6608. **)
  6609. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6610. VAR type: SyntaxTree.Type;
  6611. BEGIN
  6612. type := ResolveType(builtinProcedure.type);
  6613. END VisitBuiltin;
  6614. (* nopov *)
  6615. (** check and resolve operator
  6616. - operators are first checked as procedures
  6617. - then additional operator-specific checks are done
  6618. - note that only module-scope operators are checked here
  6619. (operators in a record scope are only allowed in the context of
  6620. array-structured object types and checked in 'ResolveArrayStructure')
  6621. - also note that inter-operator conformity is not checked here
  6622. **)
  6623. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6624. VAR
  6625. procedureType: SyntaxTree.ProcedureType;
  6626. leftType, rightType: SyntaxTree.Type;
  6627. identifierNumber: LONGINT; position: Position;
  6628. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6629. modifiers: SyntaxTree.Modifier;
  6630. (** whether a type is locally defined in the current module scope
  6631. for arrays, the base type must be locally defined **)
  6632. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6633. BEGIN
  6634. IF type = NIL THEN
  6635. RETURN FALSE
  6636. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6637. RETURN TRUE
  6638. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6639. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6640. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6641. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6642. ELSE
  6643. RETURN FALSE
  6644. END
  6645. END IsLocallyDefined;
  6646. BEGIN
  6647. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6648. procedureType := operator.type(SyntaxTree.ProcedureType);
  6649. modifiers := procedureType.modifiers;
  6650. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6651. CheckModifiers(modifiers, TRUE);
  6652. VisitProcedure(operator);
  6653. IF operator.scope IS SyntaxTree.RecordScope THEN
  6654. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6655. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6656. IF identifierNumber = -1 THEN
  6657. Error(operator.position, "operator with unknown identifier")
  6658. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6659. Error(operator.position, "identifier may not be used for operator")
  6660. ELSE
  6661. IF procedureType.numberParameters < 1 THEN
  6662. Error(operator.position, "operator without operand");
  6663. ELSIF procedureType.numberParameters > 2 THEN
  6664. Error(operator.position, "operator with more than two operands");
  6665. ELSE
  6666. (* determine operand types *)
  6667. leftType := procedureType.firstParameter.type;
  6668. IF procedureType.numberParameters > 1 THEN
  6669. rightType := procedureType.firstParameter.nextParameter.type
  6670. ELSE
  6671. rightType := NIL
  6672. END;
  6673. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6674. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6675. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6676. Error(operator.position, "none of the operands is declared in the same module")
  6677. END
  6678. END;
  6679. (* TODO: refine the checks, think about how restrictive the checks should be
  6680. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6681. They might be used for intersection, union, complement of custom object types *)
  6682. (* defaults *)
  6683. hasReturnType := TRUE;
  6684. mustBeUnary := FALSE;
  6685. mustBeBinary := FALSE;
  6686. mustReturnBoolean := FALSE;
  6687. mustReturnInteger := FALSE;
  6688. mustHaveEquitypedOperands := FALSE;
  6689. (* operator-specific exceptions *)
  6690. CASE identifierNumber OF
  6691. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6692. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6693. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6694. mustBeBinary := TRUE
  6695. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6696. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6697. | Scanner.Times: mustBeBinary := TRUE
  6698. | Scanner.TimesTimes: mustBeBinary := TRUE
  6699. | Scanner.DotTimes: mustBeBinary := TRUE
  6700. | Scanner.PlusTimes: mustBeBinary := TRUE
  6701. | Scanner.Slash: mustBeBinary := TRUE
  6702. | Scanner.Backslash: mustBeBinary := TRUE
  6703. | Scanner.DotSlash: mustBeBinary := TRUE
  6704. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6705. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6706. | Scanner.Not: mustBeUnary := TRUE
  6707. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6708. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6709. | Scanner.Transpose: mustBeUnary := TRUE;
  6710. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6711. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6712. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6713. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6714. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6715. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6716. | Global.Abs: mustBeUnary := TRUE;
  6717. | Global.Ash: (* TODO: arity? *)
  6718. | Global.Cap: (* TODO: arity? *)
  6719. | Global.Chr: mustBeUnary := TRUE;
  6720. | Global.Entier: (* TODO: arity? *)
  6721. | Global.EntierH: (* TODO: arity? *)
  6722. | Global.Len: (* unary and binary *)
  6723. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6724. | Global.Max, Global.Min: (* unary and binary *)
  6725. | Global.Odd: (* TODO: arity? *)
  6726. | Global.Sum: (* TODO: arity? *)
  6727. | Global.All: (* TODO: arity? *)
  6728. | Global.Re, Global.Im:
  6729. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6730. | Scanner.Alias:
  6731. | Scanner.GreaterGreater, Scanner.LessLess:
  6732. mustBeBinary := TRUE; hasReturnType := FALSE;
  6733. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6734. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6735. END;
  6736. (* check parameter count *)
  6737. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6738. Error(operator.position,"operator is not unary")
  6739. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6740. Error(operator.position,"operator is not binary")
  6741. END;
  6742. (* check parameter types *)
  6743. (* TODO: is this used at all? *)
  6744. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6745. leftType := procedureType.firstParameter.type;
  6746. rightType := procedureType.firstParameter.nextParameter.type;
  6747. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6748. Error(operator.position, "the two operands are not of the same type")
  6749. END
  6750. END;
  6751. (* check return type *)
  6752. IF hasReturnType THEN
  6753. IF procedureType.returnType = NIL THEN
  6754. Error(operator.position, "return type required")
  6755. ELSIF mustReturnBoolean THEN
  6756. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6757. Error(operator.position,"return type is not Boolean")
  6758. END
  6759. ELSIF mustReturnInteger THEN
  6760. IF ~IsSizeType(procedureType.returnType, system.addressSize) THEN
  6761. Error(operator.position,"return type is no size type")
  6762. END
  6763. END
  6764. ELSIF procedureType.returnType # NIL THEN
  6765. Error(operator.position, "return type not allowed")
  6766. END
  6767. END
  6768. END
  6769. END
  6770. END VisitOperator;
  6771. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6772. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6773. BEGIN
  6774. IF error THEN RETURN FALSE END;
  6775. prevScope := currentScope;
  6776. prevDiagnostics := diagnostics;
  6777. diagnostics := NIL; (* suppress error output *)
  6778. currentScope := module.moduleScope;
  6779. VisitImport(x);
  6780. IF ~error THEN
  6781. module.moduleScope.AddImport(x);
  6782. x.SetScope(module.moduleScope);
  6783. END;
  6784. currentScope := prevScope;
  6785. diagnostics := prevDiagnostics;
  6786. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6787. END AddImport;
  6788. (** check and resolve import
  6789. - check for name = SYSTEM
  6790. - check for forbidden self import
  6791. - search through global import cache: already imported?
  6792. - check if already imported indirectly
  6793. - import if necessary -> set module and enter into import cache
  6794. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6795. - after this import this direct import and all indirect imports are stored in the current module's import list
  6796. **)
  6797. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6798. VAR
  6799. module: SyntaxTree.Module;
  6800. moduleScope: SyntaxTree.ModuleScope;
  6801. import,reimport: SyntaxTree.Import;
  6802. filename: FileName;
  6803. prevScope: SyntaxTree.Scope;
  6804. BEGIN
  6805. IF SymbolNeedsResolution(x) THEN
  6806. prevScope := currentScope;
  6807. x.SetType(SyntaxTree.importType);
  6808. moduleScope := currentScope.ownerModule.moduleScope;
  6809. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6810. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6811. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6812. Error(x.position,"forbidden self import");
  6813. ELSE
  6814. (* search through global import list: already imported ? *)
  6815. IF (x.module = NIL) & (importCache # NIL) THEN
  6816. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6817. ELSE import := NIL
  6818. END;
  6819. IF x.module # NIL THEN (* already imported indirectly *)
  6820. module := x.module;
  6821. ELSIF import # NIL THEN (* already in module list *)
  6822. module := import.module;
  6823. ASSERT(module # NIL);
  6824. x.SetModule(module);
  6825. ELSE (* must be imported *)
  6826. Global.ModuleFileName(x.moduleName,x.context,filename);
  6827. IF symbolFileFormat # NIL THEN
  6828. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6829. IF module = NIL THEN
  6830. ErrorSS(x.position,"could not import",filename);
  6831. IF VerboseErrorMessage THEN
  6832. Printout.Info("import",x)
  6833. END
  6834. ELSE
  6835. (*
  6836. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6837. (*! should rather be done by importer *)
  6838. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6839. checker.importCache := importCache;
  6840. checker.arrayBaseImported := arrayBaseImported;
  6841. checker.global := global;
  6842. checker.Module(module); (* semantic check *)
  6843. error := error OR checker.error;
  6844. END;
  6845. *)
  6846. (*
  6847. ASSERT(SyntaxTree.Resolved IN module.state);
  6848. *)
  6849. x.SetModule(module);
  6850. IF importCache # NIL THEN
  6851. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6852. import.SetContext(x.context);
  6853. import.SetModule(module);
  6854. importCache.AddImport(import);
  6855. END;
  6856. END;
  6857. ELSE
  6858. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6859. END;
  6860. END;
  6861. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6862. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6863. END;
  6864. import := module.moduleScope.firstImport;
  6865. WHILE(import # NIL) DO
  6866. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6867. ASSERT(currentScope # NIL);
  6868. ASSERT(currentScope.ownerModule # NIL);
  6869. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6870. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6871. Error(x.position,"recursive import");
  6872. ELSE
  6873. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6874. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6875. IF reimport = NIL THEN (* indirect import *)
  6876. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6877. reimport.SetContext(import.context);
  6878. reimport.SetModule(import.module);
  6879. moduleScope.AddImport(reimport);
  6880. reimport.SetScope(moduleScope);
  6881. ELSE
  6882. ASSERT(import.module # NIL);
  6883. reimport.SetModule(import.module); (* direct or indirect import *)
  6884. END;
  6885. END;
  6886. import := import.nextImport;
  6887. END;
  6888. END;
  6889. END;
  6890. currentScope := prevScope;
  6891. (* ELSE nothing to be done *)
  6892. x.SetState(SyntaxTree.Resolved);
  6893. END;
  6894. END VisitImport;
  6895. (*** statements ***)
  6896. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6897. VAR prev,resolved: SyntaxTree.Statement;
  6898. BEGIN
  6899. prev := resolvedStatement;
  6900. resolvedStatement := x;
  6901. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6902. activeCellsStatement := FALSE;
  6903. VStatement(x);
  6904. (* removed this, implementation restriction should be resolved by backend
  6905. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6906. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6907. END;
  6908. *)
  6909. resolved := resolvedStatement;
  6910. resolvedStatement := prev;
  6911. RETURN resolved
  6912. END ResolveStatement;
  6913. (** check and resolve statement sequence
  6914. - check all statements, replace if necessary
  6915. **)
  6916. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6917. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6918. BEGIN
  6919. IF statementSequence # NIL THEN (* else empty *)
  6920. FOR i := 0 TO statementSequence.Length()-1 DO
  6921. statement := statementSequence.GetStatement(i);
  6922. resolved := ResolveStatement(statement);
  6923. IF (resolved # statement) THEN
  6924. statementSequence.SetStatement(i,resolved);
  6925. END;
  6926. END;
  6927. END;
  6928. END StatementSequence;
  6929. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6930. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6931. - check if procedure is callable
  6932. - check return type = NIL (otherwise must be assignment statement)
  6933. **)
  6934. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6935. VAR call: SyntaxTree.Designator;
  6936. BEGIN
  6937. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6938. call := procedureCall.call;
  6939. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6940. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6941. END;
  6942. call := ResolveDesignator(call);
  6943. IF call = SyntaxTree.invalidDesignator THEN
  6944. (* error already handled *)
  6945. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6946. (* inline call in a statement *)
  6947. ELSIF ~IsCallable(call) THEN
  6948. Error(procedureCall.position,"called object is not a procedure");
  6949. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6950. Error(procedureCall.position,"calling procedure with non-void return type");
  6951. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6952. END;
  6953. procedureCall.SetCall(call);
  6954. (*
  6955. IF call = SyntaxTree.invalidDesignator THEN
  6956. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6957. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6958. IF IsOberonInline(procedure) THEN
  6959. Warning(procedure.position,"call to inline proc");
  6960. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6961. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6962. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6963. resolvedStatement := block;
  6964. RETURN;
  6965. END;
  6966. END;
  6967. *)
  6968. END VisitProcedureCallStatement;
  6969. (** check and resolve assignment LHS := RHS
  6970. - resolve LHS and RHS
  6971. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6972. - check if assignment is compatible
  6973. - check if LHS is variable (i.e. assignable)
  6974. - convert RHS if necessary
  6975. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6976. - assignment between different ASOTs
  6977. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6978. - assignment to ASOT elements:
  6979. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6980. **)
  6981. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6982. VAR
  6983. left: SyntaxTree.Designator;
  6984. right, expression: SyntaxTree.Expression;
  6985. designator: SyntaxTree.Designator;
  6986. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6987. mathArrayType: SyntaxTree.MathArrayType;
  6988. BEGIN
  6989. right := ResolveExpression(assignment.right);
  6990. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6991. left := ResolveDesignator(assignment.left);
  6992. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6993. (* error already handled *)
  6994. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6995. (* LHS is index write operator call on ASOT *)
  6996. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6997. (* necessary ?
  6998. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6999. type := procedureType.firstParameter.type;
  7000. expression := procedureCallDesignator.parameters.GetExpression(0);
  7001. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  7002. *)
  7003. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7004. ELSIF CheckVariable(left) THEN
  7005. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  7006. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7007. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7008. (* conversion done by procedure call
  7009. (* try to convert to left argument *)
  7010. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7011. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7012. procedureCallDesignator.parameters.SetExpression(1, right);
  7013. END;
  7014. *)
  7015. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7016. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  7017. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  7018. ELSIF AssignmentCompatible(left, right) THEN
  7019. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  7020. mathArrayType := MathArrayStructureOfType(left.type);
  7021. right := NewConversion(right.position, right, mathArrayType, NIL);
  7022. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  7023. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  7024. ELSE
  7025. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7026. assignment.SetLeft(left);
  7027. assignment.SetRight(right);
  7028. resolvedStatement := assignment
  7029. END
  7030. END
  7031. END
  7032. END VisitAssignment;
  7033. (** check and resolve assignment LHS := RHS
  7034. - resolve LHS and RHS
  7035. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7036. - check if assignment is compatible
  7037. - check if LHS is variable (i.e. assignable)
  7038. - convert RHS if necessary
  7039. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7040. - assignment between different ASOTs
  7041. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7042. - assignment to ASOT elements:
  7043. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7044. **)
  7045. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7046. VAR
  7047. left: SyntaxTree.Designator;
  7048. right: SyntaxTree.Expression;
  7049. inPort, outPort: SyntaxTree.PortType;
  7050. expression: SyntaxTree.Expression;
  7051. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7052. BEGIN
  7053. right := ResolveExpression(communication.right);
  7054. left := ResolveDesignator(communication.left);
  7055. communication.SetLeft(left);
  7056. communication.SetRight(right);
  7057. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7058. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7059. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7060. (* conversion done by procedure call
  7061. (* try to convert to left argument *)
  7062. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7063. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7064. procedureCallDesignator.parameters.SetExpression(1, right);
  7065. END;
  7066. *)
  7067. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7068. ELSE
  7069. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7070. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7071. (* error already handled *)
  7072. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7073. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7074. IF outPort.direction # SyntaxTree.OutPort THEN
  7075. Error(left.position,"not an out-port")
  7076. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7077. Error(left.position,"incompatible to port type");
  7078. ELSE
  7079. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7080. communication.SetRight(right)
  7081. END;
  7082. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7083. IF CheckVariable(left) THEN
  7084. IF inPort.direction # SyntaxTree.InPort THEN
  7085. Error(left.position,"not an in-port")
  7086. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7087. Error(right.position,"incompatible to port type");
  7088. END;
  7089. END;
  7090. ELSE
  7091. Error(communication.position,"unsupported stream operation");
  7092. END;
  7093. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7094. IF outPort.direction # SyntaxTree.OutPort THEN
  7095. Error(left.position,"not an out-port")
  7096. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7097. Error(left.position,"incompatible to port type");
  7098. ELSE
  7099. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7100. communication.SetRight(right)
  7101. END;
  7102. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7103. IF CheckVariable(right) THEN
  7104. IF inPort.direction # SyntaxTree.InPort THEN
  7105. Error(left.position,"not an in-port")
  7106. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7107. Error(right.position,"incompatible to port type");
  7108. END;
  7109. END;
  7110. ELSE
  7111. Error(communication.position, "unsupported operation");
  7112. END;
  7113. END;
  7114. END VisitCommunicationStatement;
  7115. (** check and resolve if/eslif part
  7116. - check condition
  7117. - check statement sequence
  7118. **)
  7119. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7120. VAR prevUnreachable, b: BOOLEAN;
  7121. BEGIN
  7122. prevUnreachable := currentIsUnreachable;
  7123. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7124. IF IsBooleanValue(ifPart.condition,b) THEN
  7125. IF b=FALSE THEN
  7126. currentIsUnreachable := TRUE
  7127. ELSIF b=TRUE THEN
  7128. true := TRUE
  7129. END;
  7130. END;
  7131. StatementSequence(ifPart.statements);
  7132. currentIsUnreachable := prevUnreachable;
  7133. END IfPart;
  7134. (** check and resolve if statement
  7135. - check if parts and else part statement sequence
  7136. **)
  7137. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7138. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7139. BEGIN
  7140. prevUnreachable := currentIsUnreachable;
  7141. ifPartTrue := FALSE;
  7142. IfPart(ifStatement.ifPart,ifPartTrue);
  7143. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7144. elsif := ifStatement.GetElsifPart(i);
  7145. IfPart(elsif,ifPartTrue);
  7146. END;
  7147. IF ifStatement.elsePart # NIL THEN
  7148. IF ifPartTrue THEN
  7149. currentIsUnreachable := TRUE
  7150. END;
  7151. StatementSequence(ifStatement.elsePart)
  7152. END;
  7153. currentIsUnreachable := prevUnreachable;
  7154. END VisitIfStatement;
  7155. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; variable: SyntaxTree.Designator);
  7156. VAR
  7157. type,variableType: SyntaxTree.Type;
  7158. withEntry: WithEntry;
  7159. symbol: SyntaxTree.Symbol
  7160. BEGIN
  7161. type := ResolveType(withPart.type);
  7162. withPart.SetType(type);
  7163. variableType := variable.type.resolved;
  7164. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7165. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7166. END;
  7167. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7168. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7169. Error(variable.position,"is not extensible designator");
  7170. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7171. Error(variable.position,"is no local variable ");
  7172. IF VerboseErrorMessage THEN
  7173. Printout.Info("variable",variable)
  7174. END;
  7175. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7176. Error(variable.position,"withguarded symbol is no type extension of ");
  7177. IF VerboseErrorMessage THEN
  7178. Printout.Info("variable",variable);
  7179. Printout.Info("type",type);
  7180. END;
  7181. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7182. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7183. Error(variable.position,"withguarded symbol is no variable ");
  7184. IF VerboseErrorMessage THEN
  7185. Printout.Info("variable",variable);
  7186. Printout.Info("type",type);
  7187. END;
  7188. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7189. Error(variable.position,"invalid change of withguarded symbol");
  7190. ELSE
  7191. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7192. NEW(withEntry);
  7193. withEntry.previous := withEntries;
  7194. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7195. withEntry.type := type;
  7196. withEntries := withEntry;
  7197. StatementSequence(withPart.statements);
  7198. withEntries := withEntries.previous;
  7199. END;
  7200. END WithPart;
  7201. (** check and resolve with statement WITH variable: type DO ... END;
  7202. - check type and variable
  7203. - check that variable type is type extension of type
  7204. - check that variable is a variable
  7205. - enter new with scope and enter guardedVariable with same name and reference to variable
  7206. - create if statement:
  7207. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7208. **)
  7209. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7210. VAR i,j: LONGINT; prevScope: SyntaxTree.Scope; variable: SyntaxTree.Designator;
  7211. BEGIN
  7212. prevScope := currentScope;
  7213. variable := ResolveDesignator(withStatement.variable);
  7214. withStatement.SetVariable(variable);
  7215. FOR i := 0 TO withStatement.WithParts()-1 DO
  7216. WithPart(withStatement.GetWithPart(i),variable);
  7217. END;
  7218. FOR i := 0 TO withStatement.WithParts()-1 DO
  7219. FOR j := i+1 TO withStatement.WithParts()-1 DO
  7220. IF IsTypeExtension(withStatement.GetWithPart(i).type, withStatement.GetWithPart(j).type) THEN
  7221. Error(withStatement.GetWithPart(j).position, "unreachable extended type");
  7222. END;
  7223. END;
  7224. END;
  7225. IF withStatement.elsePart # NIL THEN
  7226. StatementSequence(withStatement.elsePart)
  7227. END;
  7228. currentScope := prevScope;
  7229. END VisitWithStatement;
  7230. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7231. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7232. - check 'first' < 'last' and no overlaps between different case labels
  7233. - check statement sequence
  7234. **)
  7235. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  7236. VAR
  7237. i: LONGINT;
  7238. position: Position;
  7239. expression, left, right: SyntaxTree.Expression;
  7240. expressionType: SyntaxTree.Type;
  7241. l, r: Basic.Integer;
  7242. cl, cr: CHAR;
  7243. thiscases: SyntaxTree.CaseConstant;
  7244. BEGIN
  7245. thiscases := NIL;
  7246. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7247. expression := casePart.elements.GetExpression(i);
  7248. position := expression.position;
  7249. (* set context of range *)
  7250. IF expression IS SyntaxTree.RangeExpression THEN
  7251. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7252. END;
  7253. expression := ResolveExpression(expression);
  7254. IF expression = SyntaxTree.invalidExpression THEN
  7255. (* error already reported *)
  7256. expressionType := SyntaxTree.invalidType;
  7257. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7258. (* read out 'first' and 'last' *)
  7259. left := expression(SyntaxTree.RangeExpression).first;
  7260. right := expression(SyntaxTree.RangeExpression).last;
  7261. (* guaranteed by VisitRangeExpression: *)
  7262. ASSERT((left # NIL) & (right # NIL));
  7263. ASSERT(left.type.resolved = right.type.resolved);
  7264. left := CompatibleConversion(left.position, left, type);
  7265. right := CompatibleConversion(right.position, right, type);
  7266. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7267. expression(SyntaxTree.RangeExpression).SetLast(right);
  7268. expressionType := RegularType(position,left.type);
  7269. ELSE
  7270. expression := ConstantExpression(expression);
  7271. expression := CompatibleConversion(expression.position, expression, type);
  7272. (*
  7273. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7274. left := Global.NewCharacterValue(system,expression.position,cl);
  7275. expression := casePart.elements.GetExpression(i);
  7276. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7277. expression := left
  7278. END;
  7279. *)
  7280. casePart.elements.SetExpression(i,expression);
  7281. left := expression; right := expression;
  7282. expressionType := RegularType(position,expression.type)
  7283. END;
  7284. IF (expressionType = SyntaxTree.invalidType) THEN
  7285. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7286. Error(position, "inadmissible case label");
  7287. expression := SyntaxTree.invalidExpression;
  7288. ELSE
  7289. l := 0; r := 0;
  7290. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7291. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7292. l := ORD(cl); r := ORD(cr);
  7293. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7294. ELSE
  7295. expression := SyntaxTree.invalidExpression
  7296. END;
  7297. IF expression # SyntaxTree.invalidExpression THEN
  7298. IF l>r THEN
  7299. Error(position, "empty case label")
  7300. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7301. Error(position, "duplicate case label");
  7302. ELSE
  7303. IF l < min THEN min := l END;
  7304. IF r > max THEN max := r END;
  7305. END;
  7306. END;
  7307. END;
  7308. casePart.elements.SetExpression(i,expression);
  7309. END;
  7310. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7311. casePart.SetConstants(thiscases);
  7312. StatementSequence(casePart.statements);
  7313. END CasePart;
  7314. (** check and resolve case statement CASE variable OF ... END;
  7315. - check variable
  7316. - check case parts
  7317. **)
  7318. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7319. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7320. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer;
  7321. BEGIN
  7322. expression := ResolveExpression(caseStatement.variable);
  7323. type := RegularType(expression.position,expression.type);
  7324. IF type = SyntaxTree.invalidType THEN
  7325. expression := SyntaxTree.invalidExpression;
  7326. ELSIF IsIntegerType(type) THEN
  7327. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7328. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7329. (*
  7330. expression := Global.NewCharacterValue(system,expression.position,ch);
  7331. *)
  7332. type := expression.type;
  7333. ELSIF IsCharacterType(type) THEN
  7334. ELSIF IsEnumerationType(type) THEN
  7335. ELSE
  7336. Error(caseStatement.variable.position,"variable must be integer or character type");
  7337. expression := SyntaxTree.invalidExpression;
  7338. END;
  7339. caseStatement.SetVariable(expression);
  7340. caseList := NIL;
  7341. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  7342. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7343. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7344. END;
  7345. caseStatement.SetMinMax(min,max);
  7346. StatementSequence(caseStatement.elsePart);
  7347. IF expression.resolved # NIL THEN
  7348. IF IsCharacterValue(expression,ch) THEN
  7349. l := ORD(ch)
  7350. ELSIF IsIntegerValue(expression,l) THEN
  7351. END;
  7352. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7353. END;
  7354. END VisitCaseStatement;
  7355. (** check and resolve while statement
  7356. - check condition
  7357. - check statement sequence
  7358. **)
  7359. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7360. VAR prevIsUnreachable,b: BOOLEAN;
  7361. BEGIN
  7362. prevIsUnreachable := currentIsUnreachable;
  7363. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7364. IF IsBooleanValue(whileStatement.condition,b) THEN
  7365. IF b=FALSE THEN
  7366. currentIsUnreachable := TRUE
  7367. END;
  7368. END;
  7369. StatementSequence(whileStatement.statements);
  7370. currentIsUnreachable := prevIsUnreachable
  7371. END VisitWhileStatement;
  7372. (** check and resolve repeat statement
  7373. - check condition
  7374. - check statement sequence
  7375. **)
  7376. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7377. BEGIN
  7378. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7379. StatementSequence(repeatStatement.statements);
  7380. END VisitRepeatStatement;
  7381. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7382. VAR withEntry: WithEntry;
  7383. BEGIN
  7384. withEntry := withEntries;
  7385. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7386. withEntry := withEntry.previous
  7387. END;
  7388. IF withEntry = NIL THEN RETURN FALSE
  7389. ELSE
  7390. type := withEntry.type;
  7391. RETURN TRUE
  7392. END;
  7393. END GetGuard;
  7394. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7395. - check that variable is an integer variable
  7396. - check that from is integer typed with compatible type
  7397. - check that to has compatible type
  7398. - check that by is constant integer with compatible type
  7399. **)
  7400. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7401. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7402. BEGIN
  7403. designator := ResolveDesignator(forStatement.variable);
  7404. type := SyntaxTree.invalidType;
  7405. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7406. designator := SyntaxTree.invalidDesignator;
  7407. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7408. Error(designator.position,"control variable of non-integer type");
  7409. designator := SyntaxTree.invalidDesignator;
  7410. ELSIF CheckVariable(designator) THEN
  7411. type := designator.type;
  7412. END;
  7413. forStatement.SetVariable(designator);
  7414. expression := ResolveExpression(forStatement.from);
  7415. IF expression = SyntaxTree.invalidExpression THEN
  7416. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7417. Error(expression.position,"start value of incompatible type");
  7418. expression := SyntaxTree.invalidExpression;
  7419. ELSIF type # SyntaxTree.invalidType THEN
  7420. expression := NewConversion(expression.position,expression,type,NIL)
  7421. END;
  7422. forStatement.SetFrom(expression);
  7423. expression := ResolveExpression(forStatement.to);
  7424. IF expression = SyntaxTree.invalidExpression THEN
  7425. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7426. Error(expression.position,"end value of incompatible type");
  7427. expression := SyntaxTree.invalidExpression;
  7428. ELSIF type # SyntaxTree.invalidType THEN
  7429. expression := NewConversion(expression.position,expression,type,NIL)
  7430. END;
  7431. forStatement.SetTo(expression);
  7432. IF forStatement.by # NIL THEN
  7433. expression := ConstantInteger(forStatement.by);
  7434. ELSE
  7435. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7436. END;
  7437. IF expression = SyntaxTree.invalidExpression THEN
  7438. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7439. Error(expression.position,"step value of incompatible type");
  7440. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  7441. Error(expression.position,"invalid step value");
  7442. ELSIF type # SyntaxTree.invalidType THEN
  7443. expression := NewConversion(expression.position,expression,type,NIL)
  7444. END;
  7445. forStatement.SetBy(expression);
  7446. StatementSequence(forStatement.statements);
  7447. END VisitForStatement;
  7448. (** check and resolve loop statement LOOP StatementSequence END
  7449. - check statement sequence
  7450. **)
  7451. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7452. BEGIN
  7453. StatementSequence(loopStatement.statements)
  7454. END VisitLoopStatement;
  7455. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7456. BEGIN
  7457. StatementSequence(exitableBlock.statements);
  7458. END VisitExitableBlock;
  7459. (** check and resolve exit statement EXIT
  7460. - check that exit is within LOOP statement block
  7461. **)
  7462. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7463. VAR outer: SyntaxTree.Statement;
  7464. BEGIN
  7465. outer := exitStatement.outer;
  7466. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7467. outer := outer.outer;
  7468. END;
  7469. IF outer = NIL THEN
  7470. Error(exitStatement.position,"exit statement not within loop statement");
  7471. END;
  7472. END VisitExitStatement;
  7473. (** check and resolve return statement RETURN [expression]
  7474. - check expression (if any)
  7475. - check if in procedure scope
  7476. - if in procedure scope then check expression compatibility
  7477. - if not in procecdure scope then check on return without expression
  7478. **)
  7479. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7480. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7481. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7482. BEGIN
  7483. position := returnStatement.position;
  7484. expression := returnStatement.returnValue;
  7485. IF expression # NIL THEN
  7486. expression := ResolveExpression(expression);
  7487. returnStatement.SetReturnValue(expression);
  7488. END;
  7489. outer := returnStatement.outer;
  7490. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7491. outer := outer.outer
  7492. END;
  7493. IF (outer # NIL) THEN
  7494. scope := outer(SyntaxTree.Body).inScope;
  7495. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7496. IF (expression # NIL) THEN
  7497. Error(position, "return statement with parameter not in procedure scope");
  7498. END;
  7499. ELSE
  7500. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7501. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7502. Error(position, "return statement in procedure that does not return");
  7503. END;
  7504. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7505. IF returnType # NIL THEN
  7506. returnType := returnType.resolved;
  7507. IF expression = NIL THEN
  7508. Error(position, "empty return type in procedure providing a return type")
  7509. ELSIF expression.type = NIL THEN
  7510. Error(position,"returned type incompatible: expression has no type");
  7511. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7512. Error(position, "return type not compatible");
  7513. IF VerboseErrorMessage THEN
  7514. Printout.Info("returnType",returnType);
  7515. Printout.Info("expression",expression);
  7516. END;
  7517. ELSE
  7518. expression := NewConversion(expression.position,expression,returnType,NIL);
  7519. returnStatement.SetReturnValue(expression);
  7520. END;
  7521. ELSIF expression # NIL THEN
  7522. Error(position, "non-empty return type in procedure providing no return type");
  7523. END;
  7524. END;
  7525. END;
  7526. END VisitReturnStatement;
  7527. (** check and resolve await statement AWAIT(condition: Expression)
  7528. - check await condition
  7529. **)
  7530. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7531. VAR condition: SyntaxTree.Expression;
  7532. BEGIN
  7533. condition := ResolveCondition(awaitStatement.condition);
  7534. IF currentIsRealtime THEN
  7535. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7536. END;
  7537. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7538. Error(awaitStatement.position,"senseless await statement with constant condition");
  7539. END;
  7540. awaitStatement.SetCondition(condition);
  7541. END VisitAwaitStatement;
  7542. PROCEDURE CheckSystemImport(position: Position);
  7543. VAR import: SyntaxTree.Import;
  7544. BEGIN
  7545. import := currentScope.ownerModule.moduleScope.firstImport;
  7546. WHILE(import # NIL) DO
  7547. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7548. RETURN;
  7549. END;
  7550. import := import.nextImport;
  7551. END;
  7552. Error(position, "forbidden code without system import");
  7553. END CheckSystemImport;
  7554. (** check and resolve code statement: do nothing, must be done by assembler
  7555. **)
  7556. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7557. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7558. BEGIN
  7559. CheckSystemImport(code.position);
  7560. FOR i := 0 TO code.inRules.Length()-1 DO
  7561. statement := code.inRules.GetStatement(i);
  7562. IF statement IS SyntaxTree.Assignment THEN
  7563. WITH statement: SyntaxTree.Assignment DO
  7564. statement.SetRight(ResolveExpression(statement.right));
  7565. END;
  7566. ELSE
  7567. Error(statement.position, "can only be assignment")
  7568. END;
  7569. END;
  7570. FOR i := 0 TO code.outRules.Length()-1 DO
  7571. statement := code.outRules.GetStatement(i);
  7572. IF statement IS SyntaxTree.Assignment THEN
  7573. WITH statement: SyntaxTree.Assignment DO
  7574. statement.SetLeft(ResolveDesignator(statement.left));
  7575. END;
  7576. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7577. (* must be a reference to some register *)
  7578. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7579. ELSE
  7580. Printout.Info("out statement ", statement);
  7581. Error(statement.position, "(out) can only be assignment")
  7582. END;
  7583. END;
  7584. END VisitCode;
  7585. (** check and set flags of a statement block
  7586. - check for multiply occurence of a flag
  7587. - check and set priority only in bodies
  7588. - check for valid names
  7589. **)
  7590. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7591. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7592. flag: LONGINT; recordBody: SyntaxTree.Body;
  7593. PROCEDURE SetProtectedRecord;
  7594. VAR scope: SyntaxTree.Scope;
  7595. BEGIN
  7596. scope := currentScope;
  7597. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7598. scope := scope.outerScope
  7599. END;
  7600. IF scope # NIL THEN
  7601. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7602. END;
  7603. END SetProtectedRecord;
  7604. BEGIN
  7605. flags := {};
  7606. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7607. recordBody := block(SyntaxTree.Body)
  7608. ELSE
  7609. recordBody := NIL
  7610. END;
  7611. blockModifier := block.blockModifiers;
  7612. WHILE(blockModifier # NIL) DO
  7613. name := blockModifier.identifier;
  7614. expression := blockModifier.expression;
  7615. position := blockModifier.position;
  7616. flag := -1;
  7617. IF name=Global.NamePriority THEN
  7618. IF expression = NIL THEN
  7619. Error(position, "missing priority expression");
  7620. ELSIF recordBody = NIL THEN
  7621. Error(position, "priority not on record body");
  7622. ELSIF recordBody.priority # NIL THEN
  7623. Error(position, "duplicate priority expression");
  7624. ELSE
  7625. recordBody.SetPriority(expression);
  7626. END;
  7627. ELSIF expression # NIL THEN
  7628. Error(expression.position,"expression not in connection with priority")
  7629. ELSIF name=Global.NameExclusive THEN
  7630. IF block.isExclusive THEN
  7631. Error(position, "duplicate exclusive flag")
  7632. END;
  7633. block.SetExclusive(TRUE); SetProtectedRecord;
  7634. ELSIF name=Global.NameActive THEN
  7635. IF recordBody = NIL THEN
  7636. Error(position, "active not in record body");
  7637. ELSIF recordBody.isActive THEN
  7638. Error(position, "duplicate active flag")
  7639. ELSE
  7640. recordBody.SetActive(TRUE); SetProtectedRecord;
  7641. END;
  7642. ELSIF name=Global.NameSafe THEN
  7643. IF recordBody = NIL THEN
  7644. Error(position, "safe not in record body");
  7645. ELSIF recordBody.isSafe THEN
  7646. Error(position, "duplicate safe flag")
  7647. ELSE
  7648. recordBody.SetSafe(TRUE);
  7649. SetProtectedRecord;
  7650. END;
  7651. ELSIF name=Global.NameRealtime THEN
  7652. IF recordBody = NIL THEN
  7653. Error(position, "realtime not in record body");
  7654. ELSIF recordBody.isRealtime THEN
  7655. Error(position, "duplicate realtime flag")
  7656. ELSE
  7657. recordBody.SetRealtime(TRUE);
  7658. block.SetRealtime(TRUE);
  7659. END;
  7660. ELSIF name=Global.NameUnchecked THEN
  7661. IF block.isUnchecked THEN
  7662. Error(position, "duplicate unchecked flag")
  7663. ELSE
  7664. block.SetUnchecked(TRUE);
  7665. END;
  7666. ELSIF (name=Global.NameUncooperative) THEN
  7667. IF block.isUncooperative THEN
  7668. Error(position, "duplicate uncooperative flag")
  7669. ELSE
  7670. block.SetUncooperative(TRUE);
  7671. END;
  7672. ELSE
  7673. Error(position, "unknown block modifier");
  7674. END;
  7675. blockModifier := blockModifier.nextModifier;
  7676. END;
  7677. END BlockFlags;
  7678. (** check and resolve statement block
  7679. - check flags (exclusive)
  7680. - check statement sequence
  7681. **)
  7682. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7683. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7684. BEGIN
  7685. BlockFlags(statementBlock);
  7686. IF statementBlock.isExclusive THEN
  7687. (* check that not in exclusive block *)
  7688. IF currentIsExclusive THEN
  7689. Error (statementBlock.position,"forbidden recursive exclusive")
  7690. ELSIF currentIsRealtime THEN
  7691. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7692. END;
  7693. END;
  7694. recentExclusive := currentIsExclusive;
  7695. recentUnreachable := currentIsUnreachable;
  7696. recentRealtime := currentIsRealtime;
  7697. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7698. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7699. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7700. StatementSequence(statementBlock.statements);
  7701. currentIsRealtime := recentRealtime;
  7702. currentIsExclusive := recentExclusive;
  7703. currentIsUnreachable := recentUnreachable;
  7704. END VisitStatementBlock;
  7705. (** check and resolve body
  7706. - check flags (active, priority, safe)
  7707. - check body and finally part
  7708. **)
  7709. PROCEDURE Body(body: SyntaxTree.Body);
  7710. BEGIN
  7711. VisitStatementBlock(body);
  7712. IF body.isActive THEN
  7713. IF ~currentIsBodyProcedure THEN
  7714. Error(body.position,"active flag not in object body");
  7715. ELSIF body.priority # NIL THEN
  7716. body.SetPriority(ConstantInteger(body.priority));
  7717. END;
  7718. ELSIF body.isSafe THEN
  7719. Error(body.position,"safe flag not in active body");
  7720. ELSIF body.priority # NIL THEN
  7721. Error(body.position,"priority flag not in active body");
  7722. END;
  7723. IF body.code # NIL THEN
  7724. CheckSystemImport(body.position);
  7725. END;
  7726. StatementSequence(body.finally)
  7727. END Body;
  7728. (*** scopes ***)
  7729. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7730. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7731. VAR duplicateSymbol: BOOLEAN;
  7732. BEGIN
  7733. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7734. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7735. Error(symbol.position,"globally defined keyword")
  7736. END;
  7737. scope.EnterSymbol(symbol,duplicateSymbol);
  7738. IF ~allowDuplicate & duplicateSymbol THEN
  7739. Error(symbol.position,"Multiply defined identifier.");
  7740. IF VerboseErrorMessage THEN
  7741. Printout.Info("multiply defined identifier",symbol);
  7742. Printout.Info("in scope",scope);
  7743. END;
  7744. END;
  7745. END Register;
  7746. (**
  7747. implementation: check and resolve an implementation part
  7748. **)
  7749. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7750. move implementation checker to a separate object ? *)
  7751. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7752. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7753. BEGIN
  7754. prevIsRealtime := currentIsRealtime;
  7755. prevIsBodyProcedure := currentIsBodyProcedure;
  7756. prevIsCellNet := currentIsCellNet;
  7757. prevScope := currentScope;
  7758. currentScope := scope;
  7759. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7760. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7761. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7762. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7763. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7764. (*
  7765. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7766. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7767. END;
  7768. *)
  7769. END;
  7770. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7771. (* module body, record bodies are wrapped into an artifical procedure *)
  7772. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7773. Body(scope(SyntaxTree.ProcedureScope).body)
  7774. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7775. Body(scope(SyntaxTree.ProcedureScope).body)
  7776. END;
  7777. END;
  7778. currentScope := prevScope;
  7779. currentIsRealtime := prevIsRealtime;
  7780. currentIsBodyProcedure := prevIsBodyProcedure;
  7781. currentIsCellNet := prevIsCellNet;
  7782. END Implementation;
  7783. (** implementation phase:
  7784. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7785. **)
  7786. PROCEDURE Implementations(x: SyntaxTree.Module);
  7787. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7788. BEGIN
  7789. prevPhase := phase;
  7790. phase := InlinePhase;
  7791. scope := x.firstScope;
  7792. WHILE(scope # NIL) DO
  7793. Implementation(scope);
  7794. scope := scope.nextScope;
  7795. END;
  7796. phase := ImplementationPhase;
  7797. scope := x.firstScope;
  7798. WHILE(scope # NIL) DO
  7799. Implementation(scope);
  7800. scope := scope.nextScope;
  7801. END;
  7802. phase := prevPhase;
  7803. END Implementations;
  7804. (** declaration phase:
  7805. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7806. - import lists (for module scopes)
  7807. - parameter list (for procedure scopes)
  7808. - constant declarations
  7809. - type declarations
  7810. - variable declarations
  7811. - procedure declarations
  7812. preformed in two stages:
  7813. - first all symbols are entered into the symbol table (with uniqueness check),
  7814. - then all symbols are resolved
  7815. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7816. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7817. phases :
  7818. 0 = before procedures
  7819. 1 = procedures and later
  7820. **)
  7821. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7822. VAR
  7823. constant: SyntaxTree.Constant;
  7824. typeDeclaration: SyntaxTree.TypeDeclaration;
  7825. declaredType: SyntaxTree.Type;
  7826. variable: SyntaxTree.Variable;
  7827. procedure: SyntaxTree.Procedure;
  7828. procedureType : SyntaxTree.ProcedureType;
  7829. prevScope: SyntaxTree.Scope;
  7830. parameter: SyntaxTree.Parameter;
  7831. import: SyntaxTree.Import;
  7832. symbol: SyntaxTree.Symbol;
  7833. prevPhase: LONGINT;
  7834. prevError : BOOLEAN;
  7835. i: LONGINT;
  7836. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7837. VAR baseType: SyntaxTree.Type; property: SyntaxTree.Property;
  7838. BEGIN
  7839. IF type.baseType # NIL THEN
  7840. baseType := type.baseType.resolved;
  7841. IF baseType IS SyntaxTree.PointerType THEN
  7842. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7843. END;
  7844. (*
  7845. IF baseType IS SyntaxTree.CellType THEN
  7846. DeclareCell(baseType(SyntaxTree.CellType));
  7847. END;
  7848. *)
  7849. END;
  7850. parameter := type.firstParameter;
  7851. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7852. (*
  7853. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7854. variable.SetType(parameter.type);
  7855. variable.SetAccess(SyntaxTree.Hidden);
  7856. variable.SetModifiers(parameter.modifiers);
  7857. currentScope.PushVariable(variable);
  7858. *)
  7859. Register(parameter,scope, FALSE);
  7860. parameter := parameter.nextParameter;
  7861. END;
  7862. property := type.firstProperty;
  7863. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7864. (*
  7865. variable := currentScope.FindVariable(property.name);
  7866. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7867. prop := variable(SyntaxTree.Property);
  7868. ELSE (* add, duplicate symbols detection later *)
  7869. prop := SyntaxTree.NewProperty(property.position, property.name);
  7870. currentScope.PushVariable(prop);
  7871. END;
  7872. prop.SetType(property.type);
  7873. prop.SetValue(property.value);
  7874. prop.SetAccess(SyntaxTree.Hidden);
  7875. *)
  7876. Register(property, scope, FALSE);
  7877. property := property.nextProperty;
  7878. END;
  7879. END DeclareCell;
  7880. BEGIN
  7881. prevError := error;
  7882. prevPhase := phase;
  7883. phase := DeclarationPhase;
  7884. prevScope := currentScope;
  7885. currentScope := scope;
  7886. error := FALSE;
  7887. IF 0 IN phases THEN
  7888. (* first enter all symbols in scope *)
  7889. IF scope IS SyntaxTree.ModuleScope THEN
  7890. (* treat imports first for a module scope, , set default context if necessary *)
  7891. import := scope(SyntaxTree.ModuleScope).firstImport;
  7892. WHILE(import # NIL) DO
  7893. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7894. Register(import, currentScope, FALSE);
  7895. import := import.nextImport;
  7896. END;
  7897. import := scope(SyntaxTree.ModuleScope).firstImport;
  7898. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7899. ResolveSymbol(import);
  7900. import := import.nextImport;
  7901. END;
  7902. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7903. (* enter parameters for a procedure scope *)
  7904. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7905. parameter := procedureType.firstParameter;
  7906. WHILE(parameter # NIL) DO
  7907. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7908. END;
  7909. parameter := procedureType.returnParameter;
  7910. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7911. parameter := procedureType.selfParameter;
  7912. IF parameter # NIL THEN
  7913. Register(parameter, currentScope, FALSE);
  7914. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7915. END;
  7916. ELSIF scope IS SyntaxTree.CellScope THEN
  7917. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7918. IF~skipImplementation THEN
  7919. import := scope(SyntaxTree.CellScope).firstImport;
  7920. WHILE(import # NIL) DO
  7921. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7922. Register(import, currentScope, FALSE);
  7923. import := import.nextImport;
  7924. END;
  7925. import := scope(SyntaxTree.CellScope).firstImport;
  7926. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7927. ResolveSymbol(import);
  7928. import := import.nextImport;
  7929. END;
  7930. END;
  7931. END;
  7932. IF error THEN RETURN END;
  7933. IF skipImplementation THEN
  7934. scope.Clear;
  7935. END;
  7936. (* constants *)
  7937. constant := scope.firstConstant;
  7938. WHILE (constant # NIL) DO
  7939. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7940. END;
  7941. (* type declarations *)
  7942. typeDeclaration := scope.firstTypeDeclaration;
  7943. WHILE (typeDeclaration # NIL) DO
  7944. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7945. END;
  7946. (* variables *)
  7947. variable := scope.firstVariable;
  7948. WHILE (variable # NIL) DO
  7949. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7950. END;
  7951. (* procedures *)
  7952. IF scope.procedures # NIL THEN
  7953. FOR i := 0 TO scope.procedures.Length()-1 DO
  7954. procedure := scope.procedures.GetProcedure(i);
  7955. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7956. IF procedureType.selfParameter = NIL THEN
  7957. scope.AddProcedure(procedure);
  7958. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7959. ELSE
  7960. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7961. IF typeDeclaration = NIL THEN
  7962. Error(procedureType.selfParameter.position, "No such type declaration");
  7963. ELSE
  7964. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7965. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7966. declaredType := typeDeclaration.declaredType;
  7967. IF declaredType IS SyntaxTree.PointerType THEN
  7968. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7969. END;
  7970. IF declaredType IS SyntaxTree.RecordType THEN
  7971. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7972. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7973. ELSE
  7974. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7975. END;
  7976. END;
  7977. END;
  7978. END;
  7979. END;
  7980. END;
  7981. (* now process all symbols without any presumption on the order *)
  7982. symbol := scope.firstSymbol;
  7983. WHILE(symbol # NIL) DO
  7984. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7985. IF (symbol IS SyntaxTree.Procedure) THEN
  7986. IF 1 IN phases THEN
  7987. ResolveSymbol(symbol);
  7988. END;
  7989. ELSE
  7990. IF 0 IN phases THEN
  7991. ResolveSymbol(symbol);
  7992. END;
  7993. END;
  7994. END;
  7995. symbol := symbol.nextSymbol;
  7996. END;
  7997. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7998. symbol := scope.firstSymbol;
  7999. WHILE symbol # NIL DO
  8000. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  8001. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  8002. pointerFixes.Add(symbol, currentScope);
  8003. END;
  8004. IF ~symbol.type.resolved.isRealtime THEN
  8005. Error(symbol.position,"symbol has no realtime type");
  8006. END;
  8007. END;
  8008. symbol := symbol.nextSymbol
  8009. END;
  8010. END;
  8011. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  8012. Error(Basic.invalidPosition,"problems during offset computation in module");
  8013. END;
  8014. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  8015. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  8016. scope.ownerModule.AddScope(scope);
  8017. END;
  8018. phase := prevPhase;
  8019. currentScope := prevScope;
  8020. error := error OR prevError;
  8021. END Declarations;
  8022. (* nopov *)
  8023. (** check if all operators from one module are compatible to the ones in the other module
  8024. - check if there are not multiple operators with the same signature
  8025. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  8026. - check for all operators whose signatures are compatible, whether the return types are compatible
  8027. note that:
  8028. - the return type is not considered to be part of the signature
  8029. - two signatures are considered compatible, if all of the operands are compatible
  8030. **)
  8031. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  8032. VAR
  8033. thisOperator, thatOperator: SyntaxTree.Operator;
  8034. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  8035. thisParameter, thatParameter: SyntaxTree.Parameter;
  8036. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8037. i: LONGINT;
  8038. BEGIN
  8039. currentScope := thisModuleScope;
  8040. hasError := FALSE;
  8041. (* go through all operators in the other module *)
  8042. thatOperator := thatModuleScope.firstOperator;
  8043. WHILE (thatOperator # NIL) & ~hasError DO
  8044. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8045. (* the other operator is accessible *)
  8046. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8047. (* the other operator is not the conversion operator *)
  8048. (* go through all operators in this module *)
  8049. thisOperator := thisModuleScope.firstOperator;
  8050. WHILE (thisOperator # NIL) & ~hasError DO
  8051. IF thisOperator # thatOperator THEN
  8052. (* the operators are not the same *)
  8053. IF thisOperator.name = thatOperator.name THEN
  8054. (* the operators share the same identifier *)
  8055. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8056. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8057. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8058. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8059. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8060. (* both operators have the same paramter count *)
  8061. thisParameter := thisProcedureType.firstParameter;
  8062. thatParameter := thatProcedureType.firstParameter;
  8063. operandsAreEqual := TRUE;
  8064. operandsAreCompatible := TRUE;
  8065. (* go through all parameters *)
  8066. FOR i := 1 TO thisProcedureType.numberParameters DO
  8067. ASSERT(thatParameter # NIL);
  8068. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8069. operandsAreEqual := FALSE;
  8070. IF TypeDistance(system, thisParameter.type, thatParameter.type, thisParameter.kind = SyntaxTree.VarParameter) =Infinity THEN
  8071. operandsAreCompatible := FALSE
  8072. END
  8073. END;
  8074. thisParameter := thisParameter.nextParameter;
  8075. thatParameter := thatParameter.nextParameter
  8076. END;
  8077. IF operandsAreEqual THEN
  8078. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8079. hasError := TRUE
  8080. ELSIF operandsAreCompatible THEN
  8081. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8082. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8083. hasError := TRUE
  8084. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8085. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8086. hasError := TRUE
  8087. END
  8088. END
  8089. END
  8090. END
  8091. END;
  8092. thisOperator := thisOperator.nextOperator
  8093. END
  8094. END
  8095. END;
  8096. thatOperator := thatOperator.nextOperator
  8097. END
  8098. END CheckInterOperatorConformity;
  8099. (** check module:
  8100. - check module declaration
  8101. - add context, if necessary
  8102. - remove module from import cache, if necessary
  8103. - check declarations
  8104. - resolve all type fixes
  8105. - check implementation (bodies)
  8106. **)
  8107. PROCEDURE Module*(x: SyntaxTree.Module);
  8108. VAR (* nopov *)
  8109. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8110. BEGIN
  8111. prevScope := currentScope;
  8112. prevIsCellNet := currentIsCellNet;
  8113. module := x;
  8114. ASSERT(x # NIL);
  8115. global := system.globalScope[x.case];
  8116. x.moduleScope.SetGlobalScope(global);
  8117. currentScope := global;
  8118. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8119. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8120. RemoveModuleFromCache(importCache,x);
  8121. Declarations(x.moduleScope, FALSE, {0,1});
  8122. FixTypes();
  8123. IF module.isCellNet THEN
  8124. currentIsCellNet := TRUE;
  8125. modifier := x.modifiers;
  8126. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8127. CheckModifiers(modifier, FALSE);
  8128. END;
  8129. (* nopov *)
  8130. IF ~error THEN
  8131. (* check if operators conform to each other within this module *)
  8132. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8133. (* go through all imports *)
  8134. import := x.moduleScope.firstImport;
  8135. WHILE import # NIL DO
  8136. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8137. (* check if all operators in this module conform to the ones of the imported module *)
  8138. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8139. END;
  8140. import := import.nextImport
  8141. END;
  8142. END;
  8143. Implementations(x);
  8144. module := NIL;
  8145. currentIsCellNet := prevIsCellNet;
  8146. currentScope := prevScope;
  8147. END Module;
  8148. END Checker;
  8149. Warnings*=OBJECT (SyntaxTree.Visitor)
  8150. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8151. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8152. BEGIN
  8153. SELF.diagnostics := diagnostics
  8154. END InitWarnings;
  8155. (** types *)
  8156. PROCEDURE Type(CONST x: SyntaxTree.Type);
  8157. BEGIN
  8158. IF SyntaxTree.Warned IN x.state THEN RETURN END;
  8159. x.SetState(SyntaxTree.Warned);
  8160. WITH x:
  8161. SyntaxTree.ArrayType DO Type(x.arrayBase);
  8162. |SyntaxTree.MathArrayType DO Type(x.arrayBase);
  8163. |SyntaxTree.PointerType DO Type(x.pointerBase);
  8164. |SyntaxTree.RecordType DO Scope(x.recordScope);
  8165. |SyntaxTree.CellType DO Scope(x.cellScope)
  8166. ELSE
  8167. END;
  8168. END Type;
  8169. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8170. VAR msg: ARRAY 256 OF CHAR;
  8171. BEGIN
  8172. Global.GetSymbolName(x,msg);
  8173. Strings.Append(msg," ");
  8174. Strings.Append(msg,text);
  8175. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8176. END Warning;
  8177. (** symbols *)
  8178. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8179. BEGIN
  8180. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8181. IF ~(x IS SyntaxTree.Parameter) THEN
  8182. Warning(x,"never used");
  8183. END;
  8184. END;
  8185. WITH x:
  8186. SyntaxTree.Procedure DO
  8187. Scope(x.procedureScope)
  8188. ELSE
  8189. END;
  8190. END Symbol;
  8191. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8192. VAR
  8193. symbol: SyntaxTree.Symbol;
  8194. BEGIN
  8195. symbol := scope.firstSymbol;
  8196. WHILE(symbol # NIL) DO
  8197. Symbol(symbol);
  8198. symbol := symbol.nextSymbol;
  8199. END;
  8200. END Scope;
  8201. PROCEDURE Module*(x: SyntaxTree.Module);
  8202. BEGIN
  8203. SELF.module := x;
  8204. Scope(x.moduleScope);
  8205. END Module;
  8206. END Warnings;
  8207. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8208. BEGIN
  8209. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8210. END IsOberonInline;
  8211. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8212. BEGIN
  8213. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8214. END Resolved;
  8215. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  8216. VAR i: LONGINT;
  8217. BEGIN
  8218. i := 1;
  8219. WHILE i < x DO
  8220. i := i *2
  8221. END;
  8222. RETURN i=x
  8223. END PowerOf2;
  8224. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8225. BEGIN
  8226. RETURN
  8227. (scope # NIL) &
  8228. (scope IS SyntaxTree.ModuleScope)
  8229. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8230. OR
  8231. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8232. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8233. END IsCellNetScope;
  8234. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8235. BEGIN
  8236. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8237. END IsCellScope;
  8238. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8239. BEGIN
  8240. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8241. RETURN (scope # NIL) & IsCellNetScope(scope)
  8242. END InCellNetScope;
  8243. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8244. BEGIN
  8245. ASSERT(size MOD system.dataUnit = 0);
  8246. RETURN size DIV system.dataUnit
  8247. END ToMemoryUnits;
  8248. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8249. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8250. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8251. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8252. BEGIN
  8253. IF t = NIL THEN
  8254. RETURN TRUE
  8255. ELSE
  8256. t := t.resolved;
  8257. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8258. END;
  8259. END TypeAllowed;
  8260. BEGIN
  8261. type := type.resolved;
  8262. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8263. RETURN FALSE
  8264. ELSE
  8265. procedureType := type(SyntaxTree.ProcedureType);
  8266. numberParameters := procedureType.numberParameters;
  8267. RETURN
  8268. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8269. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8270. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8271. END;
  8272. END GetProcedureAllowed;
  8273. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8274. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8275. VAR import: SyntaxTree.Import;
  8276. BEGIN
  8277. import := importCache.ImportByModuleName(x.name,x.context);
  8278. IF import # NIL THEN
  8279. importCache.RemoveImporters(x.name,x.context);
  8280. END;
  8281. END RemoveModuleFromCache;
  8282. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8283. (* to <- this assignment compatibility *)
  8284. VAR result: BOOLEAN;
  8285. BEGIN
  8286. IF this= NIL THEN result := (to=NIL)
  8287. ELSIF to=NIL THEN result := FALSE
  8288. ELSE
  8289. (*! will be replaced by this:
  8290. ELSE result := this.CompatibleTo(to.resolved);
  8291. *)
  8292. this := this.resolved; to := to.resolved;
  8293. IF to=SyntaxTree.invalidType THEN result := FALSE
  8294. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8295. ELSIF (to = this) OR (to.SameType(this)) THEN
  8296. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8297. ELSIF to IS SyntaxTree.BasicType THEN
  8298. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8299. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8300. result := this.CompatibleTo(to.resolved)
  8301. ELSE
  8302. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8303. END
  8304. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8305. result := to.sizeInBits >= this.sizeInBits;
  8306. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8307. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8308. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8309. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8310. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8311. result := TRUE;
  8312. ELSIF to IS SyntaxTree.AnyType THEN
  8313. 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);
  8314. ELSIF to IS SyntaxTree.ObjectType THEN
  8315. 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 *) ;
  8316. ELSIF to IS SyntaxTree.ByteType THEN
  8317. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8318. ELSIF to IS SyntaxTree.CharacterType THEN
  8319. result := IsCharacterType(this)
  8320. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8321. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8322. 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)
  8323. OR IsTensor(this)
  8324. ) THEN
  8325. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8326. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8327. result := TRUE;
  8328. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8329. result := TRUE;
  8330. ELSE
  8331. result := FALSE
  8332. END;
  8333. ELSIF IsUnsafePointer(to) & IsUnsafePointer(this) THEN
  8334. result := TRUE;
  8335. ELSIF to IS SyntaxTree.PointerType THEN
  8336. result := (this IS SyntaxTree.NilType) OR
  8337. IsUnsafePointer(to) & ( (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR IsPointerType(this) OR IsTensor(this)) OR
  8338. (IsPointerType(this) & IsTypeExtension(to,this) OR
  8339. ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this))) & (~to.isRealtime OR this.isRealtime);
  8340. ELSIF to IS SyntaxTree.ProcedureType THEN
  8341. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  8342. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8343. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8344. ELSIF to IS SyntaxTree.RecordType THEN
  8345. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8346. ELSIF to IS SyntaxTree.ArrayType THEN
  8347. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8348. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8349. ELSIF StaticArrayCompatible(to, this) THEN
  8350. result := TRUE
  8351. ELSE
  8352. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8353. END;
  8354. ELSIF to IS SyntaxTree.MathArrayType THEN
  8355. IF this IS SyntaxTree.MathArrayType THEN
  8356. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8357. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8358. result := TRUE;
  8359. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8360. result := TRUE;
  8361. ELSE
  8362. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8363. END;
  8364. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8365. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8366. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8367. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8368. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8369. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8370. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8371. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8372. ELSE
  8373. result := FALSE
  8374. END;
  8375. (* an array-structured object type is compatible to the type of its array structure *)
  8376. ELSIF IsArrayStructuredObjectType(this) THEN
  8377. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8378. ELSE
  8379. result := FALSE;
  8380. END;
  8381. ELSIF to IS SyntaxTree.StringType THEN
  8382. result := FALSE;
  8383. ELSIF to IS SyntaxTree.EnumerationType THEN
  8384. result := IsEnumerationExtension(this,to);
  8385. ELSIF to IS SyntaxTree.PortType THEN
  8386. result := SameType(to, this)
  8387. ELSE
  8388. Printout.Info("CompatibleTo",to);
  8389. HALT(100); (* implement missing type check *)
  8390. END;
  8391. END;
  8392. RETURN result
  8393. END CompatibleTo;
  8394. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8395. VAR actualBase, formalBase: SyntaxTree.Type;
  8396. BEGIN
  8397. IF SameType(formal,actual) THEN
  8398. RETURN TRUE
  8399. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8400. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8401. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8402. RETURN
  8403. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8404. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8405. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8406. & StaticArrayCompatible(formalBase,actualBase)
  8407. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8408. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8409. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8410. RETURN
  8411. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8412. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8413. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8414. & StaticArrayCompatible(formalBase,actualBase)
  8415. ELSE RETURN FALSE
  8416. END;
  8417. END StaticArrayCompatible;
  8418. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8419. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8420. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8421. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8422. BEGIN
  8423. result := SameType(formal,actual);
  8424. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8425. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8426. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8427. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8428. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8429. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8430. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8431. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8432. & TC(formalBase, actualBase);
  8433. END;
  8434. RETURN result
  8435. END TC;
  8436. BEGIN
  8437. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8438. ELSE
  8439. arrayBase := formalType.arrayBase.resolved;
  8440. IF (actualType IS SyntaxTree.StringType) THEN
  8441. result := arrayBase IS SyntaxTree.CharacterType
  8442. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8443. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8444. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8445. result := TC(formalType, actualType);
  8446. ELSE
  8447. result := (arrayBase IS SyntaxTree.ByteType)
  8448. END;
  8449. END;
  8450. RETURN result
  8451. END OpenArrayCompatible;
  8452. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8453. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8454. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8455. BEGIN
  8456. IF actualType IS SyntaxTree.MathArrayType THEN
  8457. actualArray := actualType(SyntaxTree.MathArrayType);
  8458. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8459. (*
  8460. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8461. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8462. *)
  8463. actualBase := ArrayBase(actualType,Infinity);
  8464. formalBase := ArrayBase(formalType,Infinity);
  8465. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8466. ELSE
  8467. (*
  8468. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8469. *)
  8470. formalBase := Resolved(formalType.arrayBase);
  8471. actualBase := Resolved(actualArray.arrayBase);
  8472. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8473. (*
  8474. ARRAY [k] -> ARRAY [n]
  8475. *)
  8476. result := (formalType.staticLength = actualArray.staticLength)
  8477. ELSE
  8478. result := TRUE
  8479. END;
  8480. IF ~result THEN
  8481. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8482. ELSIF actualBase = NIL THEN result := FALSE
  8483. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8484. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8485. ELSE
  8486. result := SameType(formalBase,actualBase)
  8487. END;
  8488. END;
  8489. ELSE
  8490. result := FALSE
  8491. END;
  8492. RETURN result
  8493. END MathArrayCompatible;
  8494. (**
  8495. Math Array Type distance for assignments / parameter passings of the form
  8496. from -> to
  8497. variants:
  8498. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8499. allowed:
  8500. static -> static (& size match)
  8501. static -> open
  8502. static -> tensor
  8503. open -> open
  8504. open -> tensor
  8505. open -> static
  8506. tensor -> tensor
  8507. tensor -> open
  8508. tensor -> static
  8509. **)
  8510. (*! think about the metric here: is form matching more important than element type matching? *)
  8511. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8512. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8513. BEGIN
  8514. fromBase := Resolved(from.arrayBase);
  8515. toBase := Resolved(to.arrayBase);
  8516. i := Infinity;
  8517. IF (from = to) OR (from.SameType(to)) THEN
  8518. i := 0;
  8519. ELSIF (from.form = to.form) THEN
  8520. (* static -> static, open -> open, tensor -> tensor *)
  8521. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8522. IF fromBase = toBase THEN i := 0
  8523. ELSIF toBase = NIL THEN i := 1
  8524. ELSIF fromBase = NIL THEN i := Infinity;
  8525. ELSIF toBase.SameType(fromBase) THEN i := 0
  8526. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8527. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8528. ELSE
  8529. i := TypeDistance(system,fromBase, toBase, varpar);
  8530. IF i < Infinity THEN i := i * 5 END;
  8531. END;
  8532. END;
  8533. ELSIF (to.form = SyntaxTree.Static) THEN
  8534. (* forbidden *)
  8535. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8536. (* static -> tensor, open -> tensor, tensor -> open *)
  8537. IF (toBase=fromBase) THEN i := 0;
  8538. ELSIF toBase = NIL THEN i := 1;
  8539. ELSIF toBase.SameType(fromBase) THEN i := 0
  8540. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8541. toBase := ArrayBase(toBase,Infinity);
  8542. IF (fromBase=toBase) THEN i := 0
  8543. ELSIF (toBase = NIL) THEN i:= 1
  8544. ELSIF (fromBase = NIL) THEN i := Infinity;
  8545. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8546. IF i < Infinity THEN i := i * 5 END;
  8547. END;
  8548. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8549. fromBase := ArrayBase(fromBase,Infinity);
  8550. IF (fromBase=toBase) THEN i := 0
  8551. ELSIF (toBase = NIL) THEN i := 1
  8552. ELSIF (fromBase = NIL) THEN i := Infinity;
  8553. ELSIF toBase.SameType(fromBase) THEN i := 0
  8554. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8555. IF i < Infinity THEN i := i * 5 END;
  8556. END;
  8557. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8558. IF i < Infinity THEN i := i * 5 END;
  8559. END;
  8560. IF i # Infinity THEN INC(i,2) END;
  8561. ELSIF (from.form = SyntaxTree.Static) THEN
  8562. (* static -> open *)
  8563. IF (toBase=fromBase) THEN i := 0;
  8564. ELSIF toBase = NIL THEN i := 1
  8565. ELSIF fromBase = NIL THEN i := Infinity
  8566. ELSIF toBase.SameType(fromBase) THEN i := 0
  8567. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8568. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8569. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8570. IF i < Infinity THEN i := i * 5 END;
  8571. END;
  8572. IF i # Infinity THEN INC(i,1) END;
  8573. ELSE HALT(100); (* unknown case *)
  8574. END;
  8575. RETURN i;
  8576. END MathArrayTypeDistance;
  8577. (** compute and return the distance of two array types
  8578. - return the distance of the base types
  8579. **)
  8580. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8581. VAR i: LONGINT;
  8582. BEGIN
  8583. i := Infinity;
  8584. IF from = to THEN
  8585. i := 0
  8586. ELSE
  8587. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8588. (*
  8589. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8590. i := TypeDistance(from.base, to.base);
  8591. IF i >= 0 THEN INC(i) END
  8592. ELSIF (from.mode = open) & (to.mode = open) THEN
  8593. i := TypeDistance(from.base, to.base);
  8594. *)
  8595. END;
  8596. RETURN i
  8597. END ArrayTypeDistance;
  8598. (** compute the signature distance of a procedure and an actual parameter list
  8599. - if any of the parameters are not compatible, the result is infinite
  8600. - add up and return the distance over all parameters
  8601. **)
  8602. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8603. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8604. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8605. BEGIN
  8606. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8607. result := Infinity
  8608. ELSE
  8609. formalParameter := procedureType.firstParameter;
  8610. i := 0;
  8611. result := 0;
  8612. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8613. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8614. actualParameter := actualParameters.GetExpression(i);
  8615. ASSERT(formalParameter.type # NIL);
  8616. IF (actualParameter.type = NIL) THEN distance := Infinity
  8617. ELSE
  8618. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8619. END;
  8620. IF distance = Infinity THEN
  8621. result := Infinity;
  8622. ELSE
  8623. to := formalParameter.type.resolved;
  8624. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8625. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8626. (* already handled varpar *)
  8627. (*
  8628. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8629. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8630. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8631. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8632. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8633. END;
  8634. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8635. result := Infinity
  8636. END;
  8637. *)
  8638. INC(result, distance);
  8639. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8640. INC(result, distance);
  8641. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8642. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8643. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8644. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8645. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8646. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8647. END;
  8648. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8649. result := Infinity
  8650. END;
  8651. ELSE
  8652. result := Infinity
  8653. END;
  8654. ELSE
  8655. INC(result,distance);
  8656. END;
  8657. END;
  8658. (*
  8659. Printout.Info("actual=", actualParameter);
  8660. Printout.Info("formal=", formalParameter);
  8661. TRACE(result);
  8662. *)
  8663. formalParameter := formalParameter.nextParameter; INC(i);
  8664. END;
  8665. END;
  8666. ASSERT(result >= 0);
  8667. RETURN result
  8668. END Distance;
  8669. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8670. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8671. BEGIN
  8672. IF right.numberParameters # (procedureType.numberParameters) THEN
  8673. result := Infinity
  8674. ELSE
  8675. formalParameter := procedureType.firstParameter;
  8676. rightParameter := right.firstParameter;
  8677. i := 0;
  8678. result := 0;
  8679. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8680. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8681. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8682. IF distance = Infinity THEN
  8683. result := Infinity;
  8684. ELSE
  8685. INC(result,distance);
  8686. END;
  8687. formalParameter := formalParameter.nextParameter;
  8688. rightParameter := rightParameter.nextParameter;
  8689. END;
  8690. END;
  8691. ASSERT(result >= 0);
  8692. RETURN result
  8693. END ProcedureTypeDistance;
  8694. (** compute and return the distance between two types, used for computation of signature distance
  8695. from -> to
  8696. **)
  8697. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8698. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8699. BEGIN
  8700. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8701. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8702. END;
  8703. i := Infinity;
  8704. IF from = to THEN
  8705. i := 0
  8706. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8707. ELSIF to.SameType(from) THEN
  8708. i := 0;
  8709. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8710. i := Infinity;
  8711. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8712. i := 10;
  8713. ELSIF (from IS SyntaxTree.StringType) THEN
  8714. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8715. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8716. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8717. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8718. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8719. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8720. i := 1
  8721. ELSIF (from IS SyntaxTree.NilType) THEN
  8722. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8723. (*
  8724. ELSIF (from = NoType) THEN
  8725. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8726. *)
  8727. ELSIF (from IS SyntaxTree.BasicType) THEN
  8728. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8729. IF varpar & (i # 0) THEN i := Infinity END;
  8730. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8731. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8732. ELSIF (from IS SyntaxTree.RecordType) THEN
  8733. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8734. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8735. IF to IS SyntaxTree.MathArrayType THEN
  8736. (*
  8737. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8738. i := Infinity;
  8739. ELSE
  8740. *)
  8741. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8742. (*
  8743. END;
  8744. *)
  8745. END
  8746. ELSIF (from IS SyntaxTree.PointerType) THEN
  8747. ptr := from(SyntaxTree.PointerType);
  8748. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8749. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8750. (* ELSE i := TypeDistance(ptr.base, to); *)
  8751. END
  8752. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8753. IF (to IS SyntaxTree.ProcedureType) THEN
  8754. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8755. END;
  8756. ELSIF (from IS SyntaxTree.PortType) THEN
  8757. IF (to IS SyntaxTree.PortType) THEN
  8758. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8759. i := 0;
  8760. END;
  8761. END;
  8762. (*no procedure test, procedure must be the same*)
  8763. END;
  8764. RETURN i
  8765. END TypeDistance;
  8766. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8767. BEGIN
  8768. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8769. END IsIntegerType;
  8770. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8771. BEGIN
  8772. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8773. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8774. OR IsPointerType(type)
  8775. )
  8776. END IsAddressType;
  8777. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8778. BEGIN
  8779. RETURN (type # NIL) & ((type.resolved IS SyntaxTree.IntegerType) & (type.resolved(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type.resolved IS SyntaxTree.SizeType))
  8780. END IsSizeType;
  8781. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8782. BEGIN
  8783. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8784. END IsSignedIntegerType;
  8785. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8786. BEGIN
  8787. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8788. END IsUnsignedIntegerType;
  8789. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8790. VAR result: BOOLEAN;
  8791. BEGIN
  8792. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8793. value := x.resolved(SyntaxTree.IntegerValue).value;
  8794. result := TRUE
  8795. ELSE
  8796. result := FALSE
  8797. END;
  8798. RETURN result
  8799. END IsIntegerValue;
  8800. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8801. VAR result: BOOLEAN;
  8802. BEGIN
  8803. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8804. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8805. result := TRUE
  8806. ELSE
  8807. result := FALSE
  8808. END;
  8809. RETURN result
  8810. END IsEnumerationValue;
  8811. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8812. VAR result: BOOLEAN;
  8813. BEGIN
  8814. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8815. value := x.resolved(SyntaxTree.RealValue).value;
  8816. result := TRUE
  8817. ELSE
  8818. result := FALSE
  8819. END;
  8820. RETURN result
  8821. END IsRealValue;
  8822. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8823. VAR result: BOOLEAN;
  8824. BEGIN
  8825. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8826. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8827. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8828. result := TRUE
  8829. ELSE
  8830. result := FALSE
  8831. END;
  8832. RETURN result
  8833. END IsComplexValue;
  8834. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8835. VAR result: BOOLEAN;
  8836. BEGIN
  8837. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8838. value := x.resolved(SyntaxTree.CharacterValue).value;
  8839. result := TRUE
  8840. ELSE
  8841. result := FALSE
  8842. END;
  8843. RETURN result
  8844. END IsCharacterValue;
  8845. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8846. VAR result: BOOLEAN;
  8847. BEGIN
  8848. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8849. value := x.resolved(SyntaxTree.BooleanValue).value;
  8850. result := TRUE
  8851. ELSE
  8852. result := FALSE
  8853. END;
  8854. RETURN result
  8855. END IsBooleanValue;
  8856. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8857. VAR result: BOOLEAN;
  8858. BEGIN
  8859. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8860. value := x.resolved(SyntaxTree.SetValue).value;
  8861. result := TRUE
  8862. ELSE
  8863. result := FALSE
  8864. END;
  8865. RETURN result
  8866. END IsSetValue;
  8867. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8868. VAR result: BOOLEAN;
  8869. BEGIN
  8870. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8871. value := x.resolved(SyntaxTree.StringValue).value;
  8872. result := TRUE
  8873. ELSE
  8874. result := FALSE
  8875. END;
  8876. RETURN result
  8877. END IsStringValue;
  8878. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8879. BEGIN
  8880. x := x.resolved;
  8881. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8882. END Indexable;
  8883. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8884. BEGIN
  8885. RETURN t1.SameType(t2.resolved);
  8886. END SameType;
  8887. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8888. BEGIN
  8889. IF t IS SyntaxTree.MathArrayType THEN
  8890. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8891. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8892. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8893. DEC(max);
  8894. END;
  8895. ELSIF t IS SyntaxTree.ArrayType THEN
  8896. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8897. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8898. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8899. END;
  8900. END;
  8901. RETURN t;
  8902. END ArrayBase;
  8903. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8904. BEGIN
  8905. type := type.resolved;
  8906. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8907. base := type(SyntaxTree.ArrayType).arrayBase;
  8908. RETURN TRUE;
  8909. END;
  8910. RETURN FALSE;
  8911. END IsOpenArray;
  8912. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8913. BEGIN
  8914. type := type.resolved;
  8915. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8916. base := type(SyntaxTree.ArrayType).arrayBase;
  8917. dim := type(SyntaxTree.ArrayType).staticLength;
  8918. RETURN TRUE
  8919. ELSE
  8920. RETURN FALSE
  8921. END;
  8922. END IsStaticArray;
  8923. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8924. BEGIN
  8925. type := type.resolved;
  8926. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8927. base := type(SyntaxTree.ArrayType).arrayBase;
  8928. RETURN TRUE
  8929. ELSE
  8930. RETURN FALSE
  8931. END;
  8932. END IsDynamicArray;
  8933. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8934. VAR i: LONGINT;
  8935. BEGIN
  8936. i := 0;
  8937. t := t.resolved;
  8938. IF t IS SyntaxTree.MathArrayType THEN
  8939. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8940. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8941. END;
  8942. ELSIF t IS SyntaxTree.ArrayType THEN
  8943. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8944. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8945. END;
  8946. END;
  8947. RETURN i
  8948. END Dimension;
  8949. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8950. BEGIN
  8951. RETURN expression.assignable;
  8952. END IsVariable;
  8953. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8954. BEGIN
  8955. IF (symbol IS SyntaxTree.Parameter) THEN
  8956. WITH symbol: SyntaxTree.Parameter DO
  8957. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8958. END;
  8959. ELSE
  8960. RETURN FALSE
  8961. END;
  8962. END IsVariableParameter;
  8963. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8964. VAR result: BOOLEAN;
  8965. BEGIN
  8966. IF type = NIL THEN result := FALSE
  8967. ELSE
  8968. type := type.resolved;
  8969. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8970. END;
  8971. RETURN result
  8972. END IsPointerType;
  8973. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8974. VAR result: BOOLEAN;
  8975. BEGIN
  8976. IF type = NIL THEN result := FALSE
  8977. ELSE
  8978. type := type.resolved;
  8979. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe OR
  8980. (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  8981. & type(SyntaxTree.MathArrayType).isUnsafe;
  8982. END;
  8983. RETURN result
  8984. END IsUnsafePointer;
  8985. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8986. BEGIN
  8987. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8988. END IsDisposable;
  8989. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8990. VAR result: BOOLEAN;
  8991. BEGIN
  8992. IF type = NIL THEN result := FALSE
  8993. ELSE
  8994. type := type.resolved;
  8995. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8996. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8997. result := result OR (type IS SyntaxTree.ObjectType);
  8998. END;
  8999. RETURN result
  9000. END IsPointerToRecord;
  9001. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  9002. VAR result: BOOLEAN;
  9003. BEGIN
  9004. IF type = NIL THEN result := FALSE
  9005. ELSE
  9006. type := type.resolved;
  9007. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  9008. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  9009. ;
  9010. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9011. result := result OR (type IS SyntaxTree.ObjectType);
  9012. END;
  9013. RETURN result
  9014. END IsPointerToObject;
  9015. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9016. BEGIN
  9017. IF type # NIL THEN
  9018. RETURN type.resolved.hasPointers
  9019. ELSE
  9020. RETURN FALSE
  9021. END;
  9022. END ContainsPointer;
  9023. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9024. BEGIN
  9025. IF type = NIL THEN RETURN FALSE END;
  9026. type := type.resolved;
  9027. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9028. END IsStringType;
  9029. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9030. BEGIN
  9031. IF type = NIL THEN RETURN FALSE END;
  9032. type := type.resolved;
  9033. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9034. END IsCharacterType;
  9035. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9036. BEGIN
  9037. IF type = NIL THEN RETURN FALSE END;
  9038. type := type.resolved;
  9039. RETURN (type IS SyntaxTree.EnumerationType)
  9040. END IsEnumerationType;
  9041. (** cf. section "Type extension (base type)" in the language report **)
  9042. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9043. VAR result: BOOLEAN;
  9044. BEGIN
  9045. ASSERT(base # NIL); ASSERT(extension # NIL);
  9046. base := base.resolved; extension := extension.resolved;
  9047. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9048. result := TRUE;
  9049. ELSE
  9050. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9051. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9052. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9053. END;
  9054. WHILE (extension # NIL) & (extension # base) DO
  9055. IF extension IS SyntaxTree.RecordType THEN
  9056. extension := extension(SyntaxTree.RecordType).baseType;
  9057. IF (extension # NIL) THEN extension := extension.resolved END;
  9058. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9059. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9060. END;
  9061. ELSE extension := NIL;
  9062. END;
  9063. END;
  9064. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9065. END;
  9066. RETURN result
  9067. END IsTypeExtension;
  9068. (** check if base is the base enumeration type of extension **)
  9069. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9070. BEGIN
  9071. base := base.resolved; extension := extension.resolved;
  9072. WHILE (extension # NIL) & (extension # base) DO
  9073. IF extension IS SyntaxTree.EnumerationType THEN
  9074. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9075. IF extension # NIL THEN extension := extension.resolved END;
  9076. ELSE
  9077. extension := NIL
  9078. END;
  9079. END;
  9080. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9081. END IsEnumerationExtension;
  9082. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9083. BEGIN
  9084. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9085. RETURN TRUE
  9086. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9087. RETURN TRUE
  9088. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9089. RETURN TRUE
  9090. ELSE
  9091. RETURN FALSE
  9092. END
  9093. END IsCallable;
  9094. (** compute and return the distance of two record types
  9095. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9096. **)
  9097. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9098. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9099. BEGIN
  9100. i := 0;
  9101. WHILE (from # NIL) & (from # to) DO
  9102. baseType := from.baseType;
  9103. IF (baseType # NIL) THEN
  9104. baseType := baseType.resolved;
  9105. IF baseType IS SyntaxTree.PointerType THEN
  9106. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9107. END;
  9108. IF baseType IS SyntaxTree.RecordType THEN
  9109. from := baseType(SyntaxTree.RecordType);
  9110. ELSE
  9111. from := NIL;
  9112. END;
  9113. ELSE
  9114. from := NIL
  9115. END;
  9116. INC(i)
  9117. END;
  9118. IF from = NIL THEN i := Infinity END;
  9119. RETURN i
  9120. END RecordTypeDistance;
  9121. (** compute and return the distance of two pointer types **)
  9122. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9123. BEGIN
  9124. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9125. RETURN Infinity;
  9126. ELSE
  9127. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9128. END;
  9129. END PointerTypeDistance;
  9130. (** check if expression contains a symbol designator pointing to a type declaration.
  9131. - if so then enter type declaration into typeDeclaration and return true else return false
  9132. **)
  9133. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9134. VAR result: BOOLEAN;
  9135. BEGIN
  9136. result := FALSE;
  9137. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9138. result := TRUE;
  9139. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9140. END;
  9141. RETURN result
  9142. END IsTypeDesignator;
  9143. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9144. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9145. VAR result: BOOLEAN;
  9146. BEGIN
  9147. type := type.resolved;
  9148. IF type IS SyntaxTree.PointerType THEN
  9149. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9150. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9151. result := TRUE
  9152. ELSE
  9153. result := type IS SyntaxTree.RecordType
  9154. END;
  9155. RETURN result
  9156. END IsExtensibleType;
  9157. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9158. BEGIN
  9159. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9160. (d IS SyntaxTree.SymbolDesignator) &
  9161. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9162. OR
  9163. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9164. END IsUnextensibleRecord;
  9165. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9166. BEGIN
  9167. IF IsUnextensibleRecord(d) THEN
  9168. RETURN FALSE
  9169. ELSE RETURN IsExtensibleType(d.type.resolved)
  9170. END;
  9171. END IsExtensibleDesignator;
  9172. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9173. BEGIN
  9174. type := type.resolved;
  9175. IF (type IS SyntaxTree.PointerType) THEN
  9176. RETURN TRUE
  9177. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9178. RETURN TRUE
  9179. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9180. RETURN TRUE
  9181. ELSIF (type IS SyntaxTree.BasicType) THEN
  9182. RETURN TRUE
  9183. END;
  9184. RETURN FALSE
  9185. END IsBasicType;
  9186. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9187. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9188. BEGIN
  9189. baseType := record.baseType;
  9190. IF (baseType # NIL) THEN
  9191. baseType := baseType.resolved;
  9192. IF (baseType IS SyntaxTree.PointerType) THEN
  9193. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9194. END;
  9195. END;
  9196. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9197. recordType := baseType(SyntaxTree.RecordType);
  9198. ELSE
  9199. recordType := NIL;
  9200. END;
  9201. RETURN recordType
  9202. END RecordBase;
  9203. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9204. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9205. BEGIN
  9206. baseRecord := RecordBase(scope.ownerRecord);
  9207. IF baseRecord = NIL THEN RETURN NIL END;
  9208. scope := baseRecord.recordScope;
  9209. procedureType := procedure.type.resolved;
  9210. IF procedure IS SyntaxTree.Operator THEN
  9211. operator := scope.firstOperator;
  9212. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9213. (*
  9214. Printout.Info("not same ",procedureType);
  9215. Printout.Info("with ",operator.type);
  9216. *)
  9217. operator := operator.nextOperator;
  9218. END;
  9219. super := operator;
  9220. ELSE
  9221. super := scope.firstProcedure;
  9222. WHILE (super # NIL) & (super.name # procedure.name) DO
  9223. super := super.nextProcedure;
  9224. END;
  9225. END;
  9226. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9227. RETURN super
  9228. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9229. RETURN super
  9230. ELSE
  9231. RETURN FindSuperProcedure(scope,procedure);
  9232. END;
  9233. END FindSuperProcedure;
  9234. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9235. VAR procedure: SyntaxTree.Procedure;
  9236. BEGIN
  9237. procedure := record.recordScope.constructor;
  9238. IF procedure = NIL THEN
  9239. record := RecordBase(record);
  9240. IF record # NIL THEN
  9241. procedure := GetConstructor(record)
  9242. END;
  9243. END;
  9244. RETURN procedure;
  9245. END GetConstructor;
  9246. (* enter a case into a list of cases in a sorted way and check for collision *)
  9247. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  9248. VAR prev,this,new: SyntaxTree.CaseConstant;
  9249. BEGIN
  9250. this := root;
  9251. prev := NIL;
  9252. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9253. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9254. RETURN FALSE
  9255. ELSE
  9256. IF (this # NIL) & (this.min = max+1) THEN
  9257. this.min := min
  9258. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9259. prev.max := min
  9260. ELSE
  9261. NEW(new); new.min := min; new.max := max;
  9262. new.next := this;
  9263. IF prev = NIL THEN
  9264. root := new;
  9265. ELSE
  9266. prev.next := new
  9267. END
  9268. END;
  9269. RETURN TRUE
  9270. END;
  9271. END EnterCase;
  9272. (** generate and return a new checker object, errors are entered into diagnostics **)
  9273. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9274. VAR checker: Checker;
  9275. BEGIN
  9276. NEW(checker, diagnostics,verboseErrorMessage,cooperative,system,symbolFileFormat,importCache,backend);
  9277. RETURN checker
  9278. END NewChecker;
  9279. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9280. VAR warnings: Warnings;
  9281. BEGIN
  9282. NEW(warnings, diagnostics); RETURN warnings;
  9283. END NewWarnings;
  9284. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9285. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9286. END IsRangeType;
  9287. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9288. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9289. END IsMathArrayType;
  9290. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9291. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9292. END IsArrayType;
  9293. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9294. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9295. END IsComplexType;
  9296. (** if a type is an array-structured object type *)
  9297. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9298. VAR recordType: SyntaxTree.RecordType;
  9299. BEGIN
  9300. IF type = NIL THEN
  9301. RETURN FALSE
  9302. ELSE
  9303. type := type.resolved;
  9304. IF type IS SyntaxTree.PointerType THEN
  9305. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9306. IF type IS SyntaxTree.RecordType THEN
  9307. recordType := type(SyntaxTree.RecordType);
  9308. RETURN recordType.isObject & recordType.HasArrayStructure()
  9309. ELSE
  9310. RETURN FALSE
  9311. END
  9312. ELSE
  9313. RETURN FALSE
  9314. END
  9315. END
  9316. END IsArrayStructuredObjectType;
  9317. (** the math array structure of a type
  9318. - for math arrays: the array itself
  9319. - for pointers: the math array structure of the pointer base
  9320. - for array-structured object types: the underlying structure
  9321. - for non-math arrays and all other types: NIL
  9322. **)
  9323. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9324. VAR
  9325. result: SyntaxTree.MathArrayType;
  9326. BEGIN
  9327. IF type = NIL THEN
  9328. result := NIL
  9329. ELSE
  9330. type := type.resolved;
  9331. IF type IS SyntaxTree.PointerType THEN
  9332. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9333. END;
  9334. IF type IS SyntaxTree.MathArrayType THEN
  9335. result := type(SyntaxTree.MathArrayType)
  9336. ELSIF type IS SyntaxTree.RecordType THEN
  9337. result := type(SyntaxTree.RecordType).arrayStructure
  9338. ELSE
  9339. result := NIL
  9340. END
  9341. END;
  9342. RETURN result
  9343. END MathArrayStructureOfType;
  9344. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  9345. VAR
  9346. result: BOOLEAN;
  9347. rangeExpression: SyntaxTree.RangeExpression;
  9348. BEGIN
  9349. IF x IS SyntaxTree.RangeExpression THEN
  9350. rangeExpression := x(SyntaxTree.RangeExpression);
  9351. result := TRUE;
  9352. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9353. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9354. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9355. ELSE
  9356. result := FALSE
  9357. END;
  9358. RETURN result
  9359. END IsStaticRange;
  9360. (** whether a type is a math array of tensor form **)
  9361. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9362. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9363. END IsTensor;
  9364. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9365. BEGIN
  9366. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9367. length := type(SyntaxTree.MathArrayType).staticLength;
  9368. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9369. RETURN TRUE
  9370. ELSE
  9371. RETURN FALSE
  9372. END;
  9373. END IsStaticMathArray;
  9374. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9375. BEGIN
  9376. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9377. END SymbolHasAddress;
  9378. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9379. BEGIN
  9380. RETURN
  9381. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9382. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9383. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9384. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9385. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9386. ;
  9387. END HasAddress;
  9388. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9389. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9390. BEGIN
  9391. IF (e IS SyntaxTree.Designator) THEN
  9392. d := e(SyntaxTree.Designator);
  9393. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9394. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9395. e := d.left;
  9396. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9397. END;
  9398. IF d # NIL THEN
  9399. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9400. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9401. END;
  9402. END;
  9403. RETURN FALSE;
  9404. END IsLocalVariable;
  9405. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9406. BEGIN
  9407. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9408. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9409. ELSE
  9410. RETURN TRUE
  9411. END;
  9412. END IsStaticProcedure;
  9413. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9414. CONST OptimizeMethodTable = FALSE;
  9415. BEGIN
  9416. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9417. END InMethodTable;
  9418. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9419. BEGIN
  9420. IF type = NIL THEN RETURN FALSE
  9421. ELSE
  9422. type := type.resolved;
  9423. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9424. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9425. END
  9426. END ReturnedAsParameter;
  9427. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9428. BEGIN
  9429. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9430. END StructuredReturnType;
  9431. END FoxSemanticChecker.
  9432. System.FreeDownTo FoxSemanticChecker ~