FoxSemanticChecker.Mod 405 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267
  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. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. complexNumbersImported: BOOLEAN;
  73. phase: LONGINT;
  74. system-: Global.System;
  75. symbolFileFormat-: Formats.SymbolFileFormat;
  76. backendName-: ARRAY 32 OF CHAR;
  77. inConversion: LONGINT;
  78. (* temporary variables for the visitors
  79. they replace variables on a stack during use of the visitor pattern and may only be
  80. - set in AcceptXXX procedures
  81. - set and read in ResolveXXX procedures
  82. *)
  83. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  84. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  85. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  86. currentScope-: SyntaxTree.Scope;
  87. currentIsRealtime: BOOLEAN;
  88. currentIsUnreachable: BOOLEAN;
  89. currentIsCellNet: BOOLEAN;
  90. currentIsBodyProcedure: BOOLEAN;
  91. currentIsExclusive: BOOLEAN;
  92. global: SyntaxTree.ModuleScope;
  93. withEntries: WithEntry;
  94. activeCellsStatement: BOOLEAN;
  95. replacements*: Replacement;
  96. cellsAreObjects: BOOLEAN;
  97. variableAccessed: BOOLEAN;
  98. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  99. BEGIN
  100. SELF.diagnostics := diagnostics;
  101. SELF.useDarwinCCalls := useDarwinCCalls;
  102. SELF.cooperative := cooperative;
  103. SELF.system := system;
  104. SELF.symbolFileFormat := symbolFileFormat;
  105. error := FALSE;
  106. NEW(typeFixes);
  107. NEW(pointerFixes);
  108. resolvedType := NIL;
  109. resolvedExpression := NIL;
  110. resolvedStatement := NIL;
  111. currentScope := NIL;
  112. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  113. SELF.importCache := importCache;
  114. arrayBaseImported := FALSE;
  115. complexNumbersImported := FALSE;
  116. SELF.VerboseErrorMessage := verboseErrorMessage;
  117. global := NIL;
  118. phase := UndefinedPhase;
  119. currentIsRealtime := FALSE;
  120. currentIsUnreachable := FALSE;
  121. currentIsCellNet := FALSE;
  122. currentIsBodyProcedure := FALSE;
  123. currentIsExclusive := FALSE;
  124. withEntries := NIL;
  125. SELF.cellsAreObjects := system.cellsAreObjects;
  126. COPY(backend, backendName);
  127. inConversion := 0;
  128. END InitChecker;
  129. (** report error **)
  130. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  131. VAR errModule: SyntaxTree.Module;
  132. BEGIN
  133. ASSERT(currentScope # NIL);
  134. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  135. Basic.ErrorC(diagnostics, errModule.sourceName, position, Basic.InvalidCode, message);
  136. error := TRUE;
  137. END Error;
  138. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  139. VAR errModule: SyntaxTree.Module;
  140. BEGIN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  143. END Warning;
  144. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  145. VAR errorMessage: ARRAY 256 OF CHAR;
  146. BEGIN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  149. error := TRUE;
  150. END ErrorSS;
  151. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  152. VAR msg, msg2: ARRAY 256 OF CHAR;
  153. BEGIN
  154. COPY(msg1, msg);
  155. Strings.Append(msg, " = ");
  156. Basic.GetString(s, msg2);
  157. Strings.Append(msg, msg2);
  158. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  159. END InfoSS;
  160. (*** symbol lookup ***)
  161. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  162. **)
  163. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  164. VAR
  165. scope,baseScope: SyntaxTree.Scope;
  166. symbol, s: SyntaxTree.Symbol;
  167. ownerRecord,base: SyntaxTree.RecordType;
  168. BEGIN
  169. scope := inScope;
  170. symbol := NIL;
  171. WHILE (scope # NIL) & (symbol = NIL) DO
  172. symbol := scope.FindSymbol(name);
  173. s := NIL;
  174. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  175. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  177. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  178. END;
  179. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  180. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  181. ELSE
  182. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  183. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  184. symbol.MarkUsed;
  185. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  186. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  187. base := RecordBase(ownerRecord);
  188. IF (base # NIL) THEN
  189. baseScope := base.recordScope;
  190. symbol := Find(baseScope,name,FALSE);
  191. ELSE
  192. symbol := NIL;
  193. END;
  194. ELSE
  195. symbol := NIL;
  196. END;
  197. END;
  198. END;
  199. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  200. END;
  201. IF (symbol # NIL) THEN
  202. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  203. ASSERT(phase = DeclarationPhase);
  204. ResolveSymbol(symbol)
  205. END;
  206. symbol.MarkUsed;
  207. END;
  208. RETURN symbol
  209. END Find;
  210. (*** types ***)
  211. (** find type declaration with name qualifiedIdentifier and return resolved type
  212. - check qualified identifier prefix, set scope to module scope if appropriate
  213. - check suffix in scope
  214. **)
  215. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  216. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  217. BEGIN
  218. result := NIL;
  219. prevScope := currentScope;
  220. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  221. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  222. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  223. IF symbol(SyntaxTree.Import).module = NIL THEN
  224. Error(qualifiedIdentifier.position,"module not loaded");
  225. result := SyntaxTree.invalidType;
  226. symbol := NIL;
  227. ELSE
  228. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  229. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  230. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  231. IF VerboseErrorMessage THEN
  232. Printout.Info("scope", currentScope);
  233. Printout.Info("symbol", symbol);
  234. END;
  235. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  236. END;
  237. END;
  238. ELSE
  239. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  240. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  241. symbol := NIL;
  242. END;
  243. ELSE
  244. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  245. IF symbol = NIL THEN
  246. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  247. IF VerboseErrorMessage THEN
  248. Printout.Info("Qualident",qualifiedIdentifier);
  249. Printout.Info("in scope",currentScope) ;
  250. END;
  251. END;
  252. END;
  253. IF symbol = NIL THEN (* error already handled *)
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  257. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  258. typeDeclaration := NIL;
  259. result := SyntaxTree.invalidType;
  260. ELSE
  261. currentScope := symbol.scope;
  262. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  263. result := ResolveType(typeDeclaration.declaredType);
  264. symbol.MarkUsed;
  265. ASSERT(result # NIL);
  266. END;
  267. currentScope := prevScope;
  268. RETURN result
  269. END ResolveNamedType;
  270. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  271. If node is currently being resolved then emit a cyclic definition error.
  272. Return TRUE only if node is fully resolved.
  273. **)
  274. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  275. VAR result: BOOLEAN;
  276. BEGIN
  277. IF SyntaxTree.Resolved IN x.state THEN
  278. result := FALSE
  279. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  280. Error(x.position,"cyclic definition");
  281. result := FALSE;
  282. ELSE
  283. result := TRUE;
  284. x.SetState(SyntaxTree.BeingResolved)
  285. END;
  286. RETURN result
  287. END TypeNeedsResolution;
  288. (** Return invalid type if x is currently being resolved, return x otherwise**)
  289. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  290. BEGIN
  291. IF SyntaxTree.Resolved IN x.state THEN
  292. RETURN x
  293. ELSE
  294. RETURN SyntaxTree.invalidType
  295. END;
  296. END ResolvedType;
  297. PROCEDURE VisitType*(x: SyntaxTree.Type);
  298. BEGIN
  299. ASSERT(x = SyntaxTree.invalidType);
  300. END VisitType;
  301. (** resolve basic type **)
  302. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  303. BEGIN
  304. IF TypeNeedsResolution(x) THEN
  305. x.SetState(SyntaxTree.Resolved);
  306. END;
  307. resolvedType := ResolvedType(x)
  308. END VisitBasicType;
  309. PROCEDURE VisitByteType*(x: SyntaxTree.ByteType);
  310. BEGIN
  311. VisitBasicType(x);
  312. END VisitByteType;
  313. (** resolve character type **)
  314. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitCharacterType;
  318. PROCEDURE VisitBooleanType*(x: SyntaxTree.BooleanType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitBooleanType;
  322. PROCEDURE VisitSetType*(x: SyntaxTree.SetType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitSetType;
  326. PROCEDURE VisitAddressType*(x: SyntaxTree.AddressType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitAddressType;
  330. PROCEDURE VisitSizeType*(x: SyntaxTree.SizeType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitSizeType;
  334. PROCEDURE VisitAnyType*(x: SyntaxTree.AnyType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitAnyType;
  338. PROCEDURE VisitObjectType*(x: SyntaxTree.ObjectType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitObjectType;
  342. PROCEDURE VisitNilType*(x: SyntaxTree.NilType);
  343. BEGIN
  344. VisitBasicType(x);
  345. END VisitNilType;
  346. (** resolve integer type **)
  347. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitIntegerType;
  351. (** resolve real type **)
  352. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitFloatType;
  356. (** resolve complex type **)
  357. PROCEDURE VisitComplexType*(x: SyntaxTree.ComplexType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitComplexType;
  361. (**
  362. resolve string type: nothing to be done
  363. **)
  364. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  365. BEGIN
  366. IF TypeNeedsResolution(x) THEN
  367. x.SetState(SyntaxTree.Resolved);
  368. END;
  369. resolvedType := ResolvedType(x)
  370. END VisitStringType;
  371. (**
  372. check enumeration scope: enter symbols and check for duplicate names
  373. **)
  374. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: Basic.Integer);
  375. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: Basic.Integer; prevScope: SyntaxTree.Scope;
  376. BEGIN
  377. prevScope := currentScope;
  378. currentScope := x;
  379. e := x.firstConstant;
  380. nextHighest := highest;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  408. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: Basic.Integer;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position, "base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  479. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  480. ELSE
  481. x.SetArrayBase(pointerType);
  482. END;
  483. END;
  484. IF x.length # NIL THEN
  485. variableAccessed := FALSE;
  486. e := ResolveExpression(x.length);
  487. IF (e.resolved = NIL) THEN
  488. IF variableAccessed THEN
  489. Error(e.position, "forbidden variable access");
  490. END;
  491. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  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. IF useDarwinCCalls THEN (*fld*)
  861. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  862. ELSE
  863. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  864. END
  865. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  866. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  867. Error(position, "undefined platform calling convention");
  868. ELSE
  869. procedureType.SetCallingConvention(system.platformCallingConvention);
  870. END;
  871. END;
  872. IF HasFlag(modifiers, Global.NameNoReturn,position) THEN procedureType.SetNoReturn(TRUE) END;
  873. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(LONGINT (value)) END; (* TODO: fix explicit integer truncation *)
  874. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  875. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  876. CheckModifiers(modifiers, TRUE);
  877. modifiers := procedureType.returnTypeModifiers;
  878. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  879. CheckModifiers(modifiers, TRUE);
  880. typeFixes.Add(procedureType,currentScope);
  881. procedureType.SetHasPointers(procedureType.isDelegate);
  882. procedureType.SetState(SyntaxTree.Resolved);
  883. END;
  884. resolvedType := ResolvedType(procedureType)
  885. END VisitProcedureType;
  886. (** check and resolve record type
  887. - check base type: must be record, math array or array-structured object type
  888. - check declarations
  889. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  890. **)
  891. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  892. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  893. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  894. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  895. hasPointers: BOOLEAN;
  896. modifiers: SyntaxTree.Modifier;
  897. value: Basic.Integer;
  898. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  899. BEGIN
  900. type := type.resolved;
  901. IF (type IS SyntaxTree.PointerType) &
  902. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  903. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  904. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  905. RETURN TRUE
  906. ELSE
  907. RETURN FALSE
  908. END;
  909. END IsPointerToRecord;
  910. BEGIN
  911. IF TypeNeedsResolution(x) THEN
  912. hasPointers := FALSE;
  913. modifiers := x.modifiers;
  914. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(LONGINT(value)*system.dataUnit) (* TODO: fix explicit integer truncation *)
  915. END;
  916. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  917. CheckModifiers(modifiers, TRUE);
  918. IF x.baseType # NIL THEN
  919. position := x.baseType.position;
  920. baseType := ResolveType(x.baseType);
  921. resolved := baseType.resolved;
  922. hasPointers := hasPointers OR resolved.hasPointers;
  923. IF x.isObject THEN (* object *)
  924. ASSERT(x.pointerType # NIL);
  925. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  926. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  927. baseType := NIL
  928. ELSIF IsPointerToRecord(resolved,recordType) THEN
  929. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  930. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  931. ELSE
  932. Error(position,"object does not extend pointer to record, object or math array ")
  933. END;
  934. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  935. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  936. ELSIF IsPointerToRecord(resolved,recordType) THEN
  937. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  938. ELSIF resolved IS SyntaxTree.RecordType THEN
  939. ELSE
  940. Error(position,"pointer to record does not extend pointer to record or record")
  941. END;
  942. ELSE
  943. IF resolved IS SyntaxTree.RecordType THEN
  944. ELSE
  945. Error(position,"record does not extend record")
  946. END;
  947. END;
  948. x.SetBaseType(baseType);
  949. IF x.Level() > 15 THEN
  950. Error(position, "record/object inheritance level too high");
  951. (* note:
  952. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  953. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  954. inheritance history of a type.
  955. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  956. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  957. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  958. *)
  959. END;
  960. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  961. Error(position, "invalid inheritance of disposable types");
  962. END;
  963. END;
  964. Declarations(x.recordScope, FALSE, {0});
  965. x.SetState(SyntaxTree.Resolved);
  966. Declarations(x.recordScope, FALSE, {1});
  967. ResolveArrayStructure(x);
  968. (* computation of sizes and offsets skipped -> done in backend / system *)
  969. recordBase := x.GetBaseRecord();
  970. IF recordBase = NIL THEN numberMethods := 0
  971. ELSE numberMethods := recordBase.recordScope.numberMethods
  972. END;
  973. isRealtime := TRUE;
  974. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  975. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  976. END;
  977. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  978. WHILE symbol # NIL DO
  979. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  980. IF symbol IS SyntaxTree.Variable THEN
  981. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  982. END;
  983. IF symbol IS SyntaxTree.Procedure THEN
  984. procedure := symbol(SyntaxTree.Procedure);
  985. IF procedure.super # NIL THEN
  986. procedure.SetMethodNumber(procedure.super.methodNumber);
  987. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  988. procedure.SetMethodNumber(numberMethods);
  989. INC(numberMethods);
  990. END;
  991. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  992. Error(procedure.position,"realtime procedure in non-realtime object")
  993. END;
  994. END;
  995. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  996. Error(symbol.position,"non-realtime symbol in realtime object")
  997. END;
  998. symbol := symbol.nextSymbol;
  999. END;
  1000. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1001. x.recordScope.SetNumberMethods(numberMethods);
  1002. (* TODO: is this needed anymore? *)
  1003. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  1004. Error(x.position,"object extends a record")
  1005. END;
  1006. IF ~x.isAbstract THEN
  1007. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  1008. Error(x.position, "non-abstract object contains abstract procedure");
  1009. END;
  1010. ELSE
  1011. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  1012. Error(x.position, "abstract object does not contain an abstract method");
  1013. END;
  1014. END;
  1015. IF (x.typeDeclaration = NIL) THEN
  1016. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  1017. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  1018. (*
  1019. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  1020. AnonymousTypeDeclaration(x,name);
  1021. *)
  1022. ELSE
  1023. AnonymousTypeDeclaration(x,"Anonymous");
  1024. END;
  1025. END;
  1026. x.SetHasPointers(hasPointers);
  1027. x.SetState(SyntaxTree.Resolved);
  1028. END;
  1029. resolvedType := ResolvedType(x);
  1030. END VisitRecordType;
  1031. (** check and resolve cell type
  1032. - check base type: must be cell
  1033. - check declarations
  1034. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1035. **)
  1036. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  1037. VAR
  1038. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1039. modifier: SyntaxTree.Modifier; position: Position; value: Basic.Integer; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1040. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1041. recordBase: SyntaxTree.RecordType;
  1042. numberMethods: LONGINT;
  1043. int: Basic.Integer;
  1044. real: LONGREAL;
  1045. bool: BOOLEAN;
  1046. set: Basic.Set;
  1047. v: SyntaxTree.Expression;
  1048. str: Scanner.StringType;
  1049. atype: SyntaxTree.ArrayType;
  1050. prev: SyntaxTree.Scope;
  1051. BEGIN
  1052. IF TypeNeedsResolution(x) THEN
  1053. recordBase := NIL;
  1054. IF cellsAreObjects THEN
  1055. IF x.baseType = NIL THEN
  1056. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1057. ImportModule(qualifiedIdentifier.prefix, x.position);
  1058. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1059. x.SetBaseType(ResolveType(x.baseType));
  1060. recordBase := x.GetBaseRecord();
  1061. IF recordBase = NIL THEN
  1062. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1063. END;
  1064. ELSE
  1065. x.SetBaseType(ResolveType(x.baseType));
  1066. END;
  1067. ELSE
  1068. x.SetBaseType(ResolveType(x.baseType));
  1069. END;
  1070. IF recordBase = NIL THEN numberMethods := 0
  1071. ELSE numberMethods := recordBase.recordScope.numberMethods
  1072. END;
  1073. modifier := x.modifiers;
  1074. (*IF ~x.isCellNet THEN*)
  1075. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1076. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1077. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1078. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1079. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1080. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1081. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1082. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1083. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1084. symbol := system.activeCellsCapabilities;
  1085. WHILE symbol # NIL DO
  1086. IF HasFlag(modifier, symbol.name, position) THEN END;
  1087. symbol := symbol.nextSymbol;
  1088. END;
  1089. modifier := x.modifiers;
  1090. WHILE (modifier # NIL) DO
  1091. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1092. IF modifier.expression # NIL THEN
  1093. v := ConstantExpression(modifier.expression);
  1094. property.SetValue(v);
  1095. IF IsIntegerValue(modifier.expression, int) THEN
  1096. (*property.SetValue(modifier.expression);*)
  1097. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1098. property.SetType(system.longintType);
  1099. ELSIF IsRealValue(modifier.expression, real) THEN
  1100. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1101. property.SetType(system.longrealType);
  1102. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1103. property.SetType(system.booleanType);
  1104. ELSIF IsSetValue(modifier.expression, set) THEN
  1105. property.SetType(system.setType);
  1106. ELSIF IsStringValue(modifier.expression, str) THEN
  1107. (*property.SetValue(modifier.expression);*)
  1108. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1109. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1110. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1111. property.SetType(atype);
  1112. ELSE
  1113. Error(modifier.position, "unsupported property type");
  1114. END;
  1115. ELSE (* flag property *)
  1116. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1117. property.SetType(system.booleanType);
  1118. END;
  1119. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1120. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1121. x.AddProperty(property);
  1122. modifier := modifier.nextModifier;
  1123. END;
  1124. CheckModifiers(modifier, FALSE);
  1125. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1126. (* process parameters *)
  1127. prev := currentScope;
  1128. currentScope := x.cellScope;
  1129. parameter :=x.firstParameter;
  1130. WHILE (parameter # NIL) DO
  1131. VisitParameter(parameter);
  1132. type := parameter.type.resolved;
  1133. IF ~(type IS SyntaxTree.PortType) THEN
  1134. WHILE IsStaticArray(type, type, len) DO
  1135. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1136. END;
  1137. WHILE IsDynamicArray(type, type) DO
  1138. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1139. END;
  1140. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1141. Error(parameter.position, "invalid type, must be port or static array of port ");
  1142. END;
  1143. END;
  1144. parameter := parameter.nextParameter;
  1145. END;
  1146. currentScope := prev;
  1147. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1148. WHILE symbol # NIL DO
  1149. IF symbol IS SyntaxTree.Variable THEN
  1150. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1151. END;
  1152. symbol := symbol.nextSymbol;
  1153. END;
  1154. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1155. IF (x.typeDeclaration = NIL) THEN
  1156. AnonymousTypeDeclaration(x,"Anonymous");
  1157. END;
  1158. x.SetState(SyntaxTree.Resolved);
  1159. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1160. Warning(x.position, "Forbidden empty Body.");
  1161. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1162. Warning(x.position, "Non-empty body for an engine?");
  1163. END;
  1164. END;
  1165. resolvedType := ResolvedType(x);
  1166. END VisitCellType;
  1167. (* check if an object is an array-structured object type
  1168. - determine the array structure
  1169. - collect operators from top to bottom in the inheritance hierarchy
  1170. - check if LEN operator is declared
  1171. - determine number of possible index operators
  1172. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1173. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1174. *)
  1175. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1176. VAR
  1177. indexOperatorCount, i: LONGINT;
  1178. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1179. isTensor: BOOLEAN;
  1180. BEGIN
  1181. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1182. (* determine array structure *)
  1183. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1184. END;
  1185. IF recordType.HasArrayStructure() THEN
  1186. (* the object is an ASOT *)
  1187. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1188. (* reset array access operators *)
  1189. arrayAccessOperators.len := NIL;
  1190. arrayAccessOperators.generalRead := NIL;
  1191. arrayAccessOperators.generalWrite := NIL;
  1192. IF isTensor THEN
  1193. (* all operators of dimensionalities 1 to max *)
  1194. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1195. ELSE
  1196. (* all operators of certain dimensionality *)
  1197. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1198. END;
  1199. NEW(arrayAccessOperators.read, indexOperatorCount);
  1200. NEW(arrayAccessOperators.write, indexOperatorCount);
  1201. FOR i := 0 TO indexOperatorCount - 1 DO
  1202. arrayAccessOperators.read[i] := NIL;
  1203. arrayAccessOperators.write[i] := NIL
  1204. END;
  1205. (* collect access operators in the record scope *)
  1206. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1207. IF arrayAccessOperators.len = NIL THEN
  1208. (* TODO: think about making this operator optional for static array structures *)
  1209. Error(recordType.position, "LEN operator missing")
  1210. END;
  1211. (* show error messages *)
  1212. IF isTensor THEN
  1213. (* require ARRAY [*] OF RANGE *)
  1214. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1215. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1216. ELSE
  1217. (* forbid ARRAY [*] OF RANGE *)
  1218. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1219. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1220. (* require RANGE, RANGE, ... RANGE *)
  1221. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1222. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1223. END;
  1224. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1225. ELSE
  1226. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1227. IF recordType.recordScope.firstOperator # NIL THEN
  1228. RETURN;
  1229. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1230. END
  1231. END
  1232. END ResolveArrayStructure;
  1233. (** collect array access operators in a record scope **)
  1234. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1235. VAR
  1236. baseType: SyntaxTree.Type;
  1237. operator: SyntaxTree.Operator;
  1238. isReadOperator, isGeneralOperator: BOOLEAN;
  1239. indexListSize, indexListKind, hashValue: LONGINT;
  1240. BEGIN
  1241. (* if a parent record scope exists, collect the operators there first *)
  1242. baseType := recordScope.ownerRecord.baseType;
  1243. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1244. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1245. END;
  1246. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1247. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1248. END;
  1249. (* go through all operators in the current record scope *)
  1250. operator := recordScope.firstOperator;
  1251. WHILE operator # NIL DO
  1252. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1253. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1254. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1255. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1256. IF isGeneralOperator THEN
  1257. IF isReadOperator THEN
  1258. arrayAccessOperators.generalRead := operator
  1259. ELSE
  1260. arrayAccessOperators.generalWrite := operator
  1261. END
  1262. ELSE
  1263. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1264. IF isReadOperator THEN
  1265. arrayAccessOperators.read[hashValue] := operator
  1266. ELSE
  1267. arrayAccessOperators.write[hashValue] := operator
  1268. END
  1269. END
  1270. END
  1271. ELSE
  1272. Error(operator.position, 'invalid operator')
  1273. END;
  1274. operator := operator.nextOperator
  1275. END
  1276. END CollectArrayAccessOperators;
  1277. (** the hash value of an index operator **)
  1278. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1279. VAR result: LONGINT;
  1280. BEGIN
  1281. IF isTensor THEN
  1282. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1283. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1284. ELSE
  1285. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1286. END
  1287. ELSE
  1288. result := indexListKind
  1289. END;
  1290. RETURN result
  1291. END IndexOperatorHash;
  1292. (** 2 to the power of exponent **)
  1293. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1294. VAR result, i: LONGINT;
  1295. BEGIN
  1296. result := 1;
  1297. FOR i := 1 TO exponent DO
  1298. result := result * 2;
  1299. END;
  1300. RETURN result
  1301. END TwoToThePowerOf;
  1302. (** check if a LEN operator has a correct signature. i.e.
  1303. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF <LENTYPE>;'
  1304. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF <LENTYPE>;'
  1305. **)
  1306. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1307. VAR
  1308. procedureType: SyntaxTree.ProcedureType;
  1309. returnedArrayType: SyntaxTree.MathArrayType;
  1310. result: BOOLEAN;
  1311. BEGIN
  1312. result := FALSE;
  1313. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1314. IF (procedureType.numberParameters = 0) THEN
  1315. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1316. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1317. IF system.lenType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1318. IF returnedArrayType.form = SyntaxTree.Open THEN
  1319. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1320. result := TRUE
  1321. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1322. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1323. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1324. result := TRUE
  1325. END
  1326. END
  1327. END
  1328. END
  1329. END;
  1330. IF result THEN
  1331. (* export symbol automatically *)
  1332. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1333. ELSE
  1334. Error(operator.position, "LEN operator with invalid signature");
  1335. END;
  1336. RETURN result
  1337. END CheckLenOperator;
  1338. (** check if an index operator has a correct signature. i.e.
  1339. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1340. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1341. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1342. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1343. - determine if it is a read or write operator (existance of return type)
  1344. - check index parameters
  1345. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1346. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1347. [LONGINT] -> binary 0 -> 0
  1348. [RANGE] -> binary 1 -> 1
  1349. [LONGINT, LONGINT] -> binary 00 -> 0
  1350. [LONGINT, RANGE] -> binary 01 -> 1
  1351. [RANGE, LONGINT] -> binary 10 -> 2
  1352. [RANGE, RANGE] -> binary 11 -> 3
  1353. etc.
  1354. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1355. - for read operators, check if return type matches the type of data that is read
  1356. - for write operators, check if last parameter type matches the type of data that is written
  1357. **)
  1358. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1359. VAR
  1360. elementType, otherElementType, dataType: SyntaxTree.Type;
  1361. procedureType: SyntaxTree.ProcedureType;
  1362. mathArrayType: SyntaxTree.MathArrayType;
  1363. parameter: SyntaxTree.Parameter;
  1364. parameterCount, rangeCount, i: LONGINT;
  1365. hasTypeError: BOOLEAN;
  1366. BEGIN
  1367. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1368. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1369. (* determine if it is a read or write operator *)
  1370. isReadOperator := (procedureType.returnType # NIL);
  1371. IF isReadOperator THEN
  1372. indexListSize := parameterCount;
  1373. ELSE
  1374. indexListSize := parameterCount - 1;
  1375. END;
  1376. IF indexListSize < 1 THEN
  1377. Error(operator.position, "index operator with too few parameters");
  1378. RETURN FALSE
  1379. END;
  1380. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1381. (* general operator *)
  1382. isGeneralOperator := TRUE;
  1383. IF indexListSize > 1 THEN
  1384. Error(operator.position, "index operator with too many parameters");
  1385. RETURN FALSE
  1386. END;
  1387. (* ARRAY [*] OF RANGE*)
  1388. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1389. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1390. Error(operator.position, "index parameter not dynamic math array of range");
  1391. RETURN FALSE
  1392. END;
  1393. parameter := procedureType.firstParameter.nextParameter
  1394. ELSE
  1395. (* fixed-dim. operator *)
  1396. isGeneralOperator := FALSE;
  1397. (* check number of index parameters *)
  1398. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1399. (* for tensors, limited to a certain size *)
  1400. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1401. Error(operator.position, "too many index parameters for tensor");
  1402. RETURN FALSE
  1403. END
  1404. ELSE
  1405. (* for non-tensors, depends on dimensionality *)
  1406. IF indexListSize # arrayStructure.Dimensionality() THEN
  1407. Error(operator.position, "index parameter count does not match dimensionality");
  1408. RETURN FALSE
  1409. END
  1410. END;
  1411. (* go through all index parameters
  1412. - count the number of ranges
  1413. - determine the index list kind number
  1414. *)
  1415. indexListKind := 0;
  1416. rangeCount := 0;
  1417. parameter := procedureType.firstParameter;
  1418. FOR i := 1 TO indexListSize DO
  1419. indexListKind := indexListKind * 2;
  1420. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1421. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1422. INC(indexListKind);
  1423. INC(rangeCount)
  1424. ELSE
  1425. Error(parameter.position, "integer or range expected");
  1426. RETURN FALSE
  1427. END;
  1428. parameter := parameter.nextParameter
  1429. END;
  1430. END;
  1431. (*
  1432. - for read operators: check type of last parameter
  1433. - for write operators: check return type
  1434. *)
  1435. IF isReadOperator THEN
  1436. dataType := procedureType.returnType (* the return type *)
  1437. ELSE
  1438. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1439. END;
  1440. elementType := arrayStructure.ElementType();
  1441. hasTypeError := FALSE;
  1442. IF isGeneralOperator THEN
  1443. (* ARRAY [?] OF <Element> *)
  1444. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1445. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1446. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1447. hasTypeError := TRUE
  1448. END
  1449. ELSE
  1450. hasTypeError := TRUE
  1451. END
  1452. ELSE
  1453. IF rangeCount = 0 THEN
  1454. (* <Element> *)
  1455. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1456. ELSE
  1457. (* ARRAY [*, *, ..., *] OF <Element> *)
  1458. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1459. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1460. IF mathArrayType.IsFullyDynamic() THEN
  1461. IF mathArrayType.Dimensionality() = rangeCount THEN
  1462. otherElementType := mathArrayType.ElementType();
  1463. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1464. ELSE
  1465. hasTypeError := TRUE
  1466. END
  1467. ELSE
  1468. hasTypeError := TRUE
  1469. END
  1470. ELSE
  1471. hasTypeError := TRUE
  1472. END
  1473. END
  1474. END;
  1475. IF hasTypeError THEN
  1476. IF isReadOperator THEN
  1477. Error(operator.position, "return type does not match")
  1478. ELSE
  1479. Error(parameter.position, "type of last parameter does not match")
  1480. END;
  1481. RETURN FALSE
  1482. END;
  1483. (* export symbol automatically *)
  1484. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1485. RETURN TRUE
  1486. END CheckIndexOperator;
  1487. (** resolve all pending types (late resolving).
  1488. - type fixes are resolved at the end of the declaration phase
  1489. - type fixes may imply new type fixes that are also entered at the end of the list
  1490. **)
  1491. PROCEDURE FixTypes;
  1492. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1493. BEGIN
  1494. prevScope := currentScope;
  1495. p := typeFixes.Get(currentScope);
  1496. WHILE p # NIL DO
  1497. ASSERT(currentScope # NIL);
  1498. ASSERT(p IS SyntaxTree.Type);
  1499. IF p IS SyntaxTree.PointerType THEN
  1500. FixPointerType(p(SyntaxTree.PointerType))
  1501. ELSIF p IS SyntaxTree.ProcedureType THEN
  1502. FixProcedureType(p(SyntaxTree.ProcedureType))
  1503. ELSE
  1504. HALT(100);
  1505. END;
  1506. p := typeFixes.Get(currentScope);
  1507. END;
  1508. currentScope :=prevScope;
  1509. END FixTypes;
  1510. (**
  1511. resolve type x
  1512. - if x is nil then return nil
  1513. - if x cannot be resolved then the result is invalidType else the result is x
  1514. - the resolved type is entered into x.resolved
  1515. **)
  1516. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1517. VAR prev,resolved: SyntaxTree.Type;
  1518. BEGIN
  1519. prev := resolvedType;
  1520. resolvedType := SyntaxTree.invalidType;
  1521. IF x = NIL THEN resolvedType := NIL
  1522. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1523. END;
  1524. resolved := resolvedType;
  1525. resolvedType := prev;
  1526. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1527. RETURN resolved
  1528. END ResolveType;
  1529. (*** compatibility rules ***)
  1530. (**
  1531. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1532. **)
  1533. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1534. VAR result: SyntaxTree.Type;
  1535. BEGIN
  1536. result := SyntaxTree.invalidType;
  1537. IF type = NIL THEN Error(position, "expression of type NIL");
  1538. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1539. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1540. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1541. ELSE result := type.resolved
  1542. END;
  1543. RETURN result
  1544. END RegularType;
  1545. (** returns signature compatibility of procedure types this and to
  1546. - if not compatible then error is reported
  1547. - compatibility means type equality
  1548. **)
  1549. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1550. VAR result: BOOLEAN;
  1551. BEGIN
  1552. result := SameType(to,this);
  1553. IF ~result THEN
  1554. Error(position, "signature incompatible");
  1555. IF VerboseErrorMessage THEN
  1556. Printout.Info("this",this);
  1557. Printout.Info("to",to);
  1558. END;
  1559. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1560. Error(position, "signature incompatible: realtime flag must be inherited");
  1561. END;
  1562. RETURN result
  1563. END SignatureCompatible;
  1564. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1565. - for var parameters compatibility means same type except for
  1566. - formal is of open array of system byte
  1567. - formal is of record type
  1568. - formal is of open array type
  1569. - formal is of open math array type
  1570. - for value parameters compatibllity means assignment compatibility except for
  1571. - formal is of open array type
  1572. if compatible the return true else report error and return false
  1573. **)
  1574. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1575. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1576. BEGIN
  1577. formalType := RegularType(formal.position,formal.type);
  1578. actualType := RegularType(actual.position,actual.type);
  1579. error := FALSE;
  1580. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1581. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1582. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1583. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1584. END;
  1585. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1586. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1587. ELSIF ~IsVariable(actual) THEN
  1588. result := FALSE; error := TRUE;
  1589. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1590. Error(actual.position,"not a variable: no operator for writing");
  1591. ELSE
  1592. Error(actual.position,"is not a variable");
  1593. END;
  1594. IF VerboseErrorMessage THEN
  1595. Printout.Info("actual",actual);
  1596. Printout.Info("formal",formal);
  1597. END;
  1598. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1599. result := CompatibleTo(system,actualType,formalType);
  1600. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1601. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1602. ELSIF IsUnsafePointer(formalType) & IsUnsafePointer(actualType) THEN
  1603. result := TRUE;
  1604. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1605. IF IsArrayStructuredObjectType(actualType) THEN
  1606. actualType := MathArrayStructureOfType(actualType)
  1607. END;
  1608. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1609. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1610. Error(actual.position,"incompatible non-static actual type");
  1611. END;
  1612. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1613. Error(actual.position,"incompatible tensor (use a range expression)");
  1614. END;
  1615. ELSE
  1616. result := SameType(actualType,formalType)
  1617. END
  1618. ELSE
  1619. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1620. actualType := system.characterType;
  1621. END;
  1622. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1623. result := TRUE; (* special rule for WINAPI parameters *)
  1624. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1625. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1626. ELSE
  1627. result := CompatibleTo(system,actualType,formalType);
  1628. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1629. Error(actual.position,"incompatible non-static actual type");
  1630. END;
  1631. END;
  1632. END;
  1633. IF ~result & ~error THEN
  1634. Error(actual.position,"incompatible parameter");
  1635. IF VerboseErrorMessage THEN
  1636. Printout.Info("actual",actual);
  1637. Printout.Info("formal",formal);
  1638. END;
  1639. END;
  1640. RETURN result
  1641. END ParameterCompatible;
  1642. (** check compatibility for expressions of the form left := right
  1643. - if compatible then return true else error report and return false
  1644. - check if left is variable
  1645. - check compatiblity
  1646. **)
  1647. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1648. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1649. BEGIN
  1650. result := FALSE;
  1651. leftType := RegularType(left.position,left.type);
  1652. rightType := RegularType(right.position,right.type);
  1653. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1654. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1655. END;
  1656. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1657. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1658. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1659. (* error already handled *)
  1660. result := TRUE;
  1661. ELSIF ~IsVariable(left) THEN
  1662. Error(left.position,"is not a variable");
  1663. IF VerboseErrorMessage THEN
  1664. Printout.Info("left",left);
  1665. Printout.Info("right",right);
  1666. END;
  1667. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1668. result := TRUE;
  1669. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1670. result := TRUE
  1671. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1672. Error(left.position,"incompatible assignment");
  1673. IF VerboseErrorMessage THEN
  1674. Printout.Info("left",left);
  1675. Printout.Info("right",right);
  1676. END;
  1677. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1678. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1679. Error(right.position,"forbidden assignment of a nested procedure");
  1680. ELSE
  1681. result := TRUE
  1682. END;
  1683. RETURN result
  1684. END AssignmentCompatible;
  1685. (*** values ***)
  1686. (** check and resolve integer value **)
  1687. PROCEDURE VisitIntegerValue*(value: SyntaxTree.IntegerValue);
  1688. VAR integer: Basic.Integer;
  1689. BEGIN
  1690. integer := value(SyntaxTree.IntegerValue).value;
  1691. value.SetType(Global.GetIntegerType(system,integer));
  1692. resolvedExpression := value
  1693. END VisitIntegerValue;
  1694. (** check and resolve real value **)
  1695. PROCEDURE VisitRealValue*(value: SyntaxTree.RealValue);
  1696. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1697. BEGIN
  1698. subtype := value(SyntaxTree.RealValue).subtype;
  1699. IF subtype = Scanner.Real THEN
  1700. type := system.realType
  1701. ELSIF subtype = Scanner.Longreal THEN
  1702. type := system.longrealType
  1703. ELSE
  1704. HALT(100)
  1705. END;
  1706. value.SetType(type);
  1707. resolvedExpression := value
  1708. END VisitRealValue;
  1709. (** check and resolve complex value **)
  1710. PROCEDURE VisitComplexValue*(value: SyntaxTree.ComplexValue);
  1711. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1712. BEGIN
  1713. subtype := value(SyntaxTree.ComplexValue).subtype;
  1714. IF subtype = Scanner.Real THEN
  1715. type := system.complexType
  1716. ELSIF subtype = Scanner.Longreal THEN
  1717. type := system.longcomplexType
  1718. ELSE
  1719. HALT(100)
  1720. END;
  1721. value.SetType(type);
  1722. resolvedExpression := value
  1723. END VisitComplexValue;
  1724. (** check and resolve set value **)
  1725. PROCEDURE VisitSetValue*(value: SyntaxTree.SetValue);
  1726. BEGIN
  1727. value.SetType(system.setType);
  1728. resolvedExpression := value
  1729. END VisitSetValue;
  1730. (** check and resolve set value **)
  1731. PROCEDURE VisitMathArrayValue*(value: SyntaxTree.MathArrayValue);
  1732. BEGIN
  1733. value.SetType(SyntaxTree.invalidType);
  1734. resolvedExpression := value
  1735. END VisitMathArrayValue;
  1736. (** check and resolve boolean value **)
  1737. PROCEDURE VisitBooleanValue*(value: SyntaxTree.BooleanValue);
  1738. BEGIN
  1739. value.SetType(system.booleanType);
  1740. resolvedExpression := value
  1741. END VisitBooleanValue;
  1742. (** check and resolve string value **)
  1743. PROCEDURE VisitStringValue*(value: SyntaxTree.StringValue);
  1744. BEGIN
  1745. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1746. resolvedExpression := value
  1747. END VisitStringValue;
  1748. (** check and resolve character value **)
  1749. PROCEDURE VisitCharacterValue*(value: SyntaxTree.CharacterValue);
  1750. BEGIN
  1751. value.SetType(system.characterType);
  1752. resolvedExpression := value
  1753. END VisitCharacterValue;
  1754. (** check and resolve nil value **)
  1755. PROCEDURE VisitNilValue*(value: SyntaxTree.NilValue);
  1756. BEGIN
  1757. value.SetType(system.nilType);
  1758. resolvedExpression := value
  1759. END VisitNilValue;
  1760. (** check and resolve enumerator value **)
  1761. PROCEDURE VisitEnumerationValue*(value: SyntaxTree.EnumerationValue);
  1762. BEGIN
  1763. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1764. ASSERT(value.type # NIL);
  1765. resolvedExpression := value
  1766. END VisitEnumerationValue;
  1767. (*** expressions ***)
  1768. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1769. - check all elements on integer type
  1770. - if element range is constant, then check lower and upper bound
  1771. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1772. if an error occurs then report error and return invalidExpression
  1773. **)
  1774. PROCEDURE VisitSet*(set: SyntaxTree.Set);
  1775. VAR
  1776. i: LONGINT;
  1777. element: SyntaxTree.Expression;
  1778. constant: BOOLEAN;
  1779. elements: SyntaxTree.ExpressionList;
  1780. s: Basic.Set;
  1781. result: SyntaxTree.Expression;
  1782. value: SyntaxTree.Value;
  1783. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1784. VAR
  1785. left, right: SyntaxTree.Expression;
  1786. elementResult: SyntaxTree.Expression;
  1787. leftInteger, rightInteger: Basic.Integer;
  1788. BEGIN
  1789. (* set context of range *)
  1790. IF element IS SyntaxTree.RangeExpression THEN
  1791. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1792. END;
  1793. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1794. IF elementResult = SyntaxTree.invalidExpression THEN
  1795. (* error already reported *)
  1796. constant := FALSE
  1797. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1798. (* the element is a range expression *)
  1799. (* extract left and right hand side of range *)
  1800. left := elementResult(SyntaxTree.RangeExpression).first;
  1801. right := elementResult(SyntaxTree.RangeExpression).last;
  1802. (* guaranteed by VisitRangeExpression: *)
  1803. ASSERT((left # NIL) & (right # NIL));
  1804. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1805. ELSE
  1806. (* the element is not a range expression *)
  1807. (* check type and add conversion if needed *)
  1808. IF IsIntegerType(elementResult.type.resolved) THEN
  1809. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1810. ELSE
  1811. Error(elementResult.position, "non integer element in set");
  1812. elementResult := SyntaxTree.invalidExpression;
  1813. constant := FALSE
  1814. END;
  1815. left := elementResult;
  1816. right := elementResult
  1817. END;
  1818. IF elementResult # SyntaxTree.invalidExpression THEN
  1819. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1820. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1821. Error(left.position,"not allowed set integer value");
  1822. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1823. Error(right.position,"not allowed set integer value");
  1824. END
  1825. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1826. Error(right.position,"not allowed set integer value");
  1827. ELSE
  1828. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1829. s := {};
  1830. ELSE
  1831. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1832. IF leftInteger < 0 THEN leftInteger := 0 END;
  1833. (*!!!!!!!!! this is a hack !!!!!!! *)
  1834. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1835. extends the range x..31 to x..63 !!!!!! *)
  1836. s := s + {leftInteger..rightInteger};
  1837. END;
  1838. END;
  1839. ELSE
  1840. constant := FALSE;
  1841. END
  1842. END;
  1843. RETURN elementResult
  1844. END CheckElement;
  1845. BEGIN
  1846. result := set; constant := TRUE; s := {}; elements := set.elements;
  1847. IF elements # NIL THEN
  1848. FOR i := 0 TO elements.Length()-1 DO
  1849. element := elements.GetExpression(i);
  1850. element := CheckElement(element);
  1851. IF element = SyntaxTree.invalidExpression THEN
  1852. result := SyntaxTree.invalidExpression
  1853. END;
  1854. elements.SetExpression(i,element);
  1855. END;
  1856. END;
  1857. IF constant THEN
  1858. value := Global.NewSetValue(system,set.position,s);
  1859. result.SetResolved(value);
  1860. result.SetType(value.type);
  1861. ELSE
  1862. result.SetType(system.setType);
  1863. END;
  1864. (* optimization possible
  1865. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1866. left this to the programmer...
  1867. *)
  1868. resolvedExpression := result;
  1869. END VisitSet;
  1870. (*
  1871. old variant: quite generic but needs better conversion handling, do this?
  1872. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1873. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1874. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1875. BEGIN
  1876. type := NIL;
  1877. numberElements := x.elements.Length();
  1878. FOR i := 0 TO numberElements-1 DO
  1879. expression := x.elements.GetExpression(i);
  1880. position := expression.position;
  1881. expression := ResolveExpression(x.elements.GetExpression(i));
  1882. x.elements.SetExpression(i,de);
  1883. IF type = NIL THEN
  1884. type := expression.type;
  1885. ELSIF CompatibleTo(system,expression.type,type) THEN
  1886. (* ok *)
  1887. ELSIF CompatibleTo(system,type,expression.type) THEN
  1888. type := expression.type
  1889. ELSE
  1890. Error(expression.position, "incompatible element types");
  1891. type := SyntaxTree.invalidType;
  1892. END;
  1893. END;
  1894. isValue := TRUE;
  1895. FOR i := 0 TO numberElements-1 DO
  1896. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1897. x.elements.SetExpression(i,expression);
  1898. isValue := isValue & (expression.resolved # NIL);
  1899. END;
  1900. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1901. arrayType.SetArrayBase(type);
  1902. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1903. IF isValue THEN
  1904. value := SyntaxTree.NewMathArrayValue(position);
  1905. value.SetElements(x.elements);
  1906. x.SetResolved(value);
  1907. END;
  1908. x.SetType(arrayType);
  1909. resolvedExpression := x;
  1910. END VisitMathArrayExpression;
  1911. *)
  1912. PROCEDURE VisitMathArrayExpression*(x: SyntaxTree.MathArrayExpression);
  1913. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1914. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1915. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1916. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1917. BEGIN
  1918. numberElements := x.elements.Length();
  1919. FOR i := 0 TO numberElements-1 DO
  1920. expression := x.elements.GetExpression(i);
  1921. IF expression IS SyntaxTree.MathArrayExpression THEN
  1922. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1923. ELSE
  1924. position := expression.position;
  1925. expression := ResolveExpression(x.elements.GetExpression(i));
  1926. x.elements.SetExpression(i,expression);
  1927. IF type = NIL THEN
  1928. type := expression.type;
  1929. ELSIF CompatibleTo(system,expression.type,type) THEN
  1930. (* ok *)
  1931. ELSIF CompatibleTo(system,type,expression.type) THEN
  1932. type := expression.type
  1933. ELSE
  1934. Error(expression.position, "incompatible element types");
  1935. type := SyntaxTree.invalidType;
  1936. END;
  1937. END;
  1938. END;
  1939. END RecursivelyFindType;
  1940. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1941. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1942. BEGIN
  1943. numberElements := x.elements.Length();
  1944. FOR i := 0 TO numberElements-1 DO
  1945. expression := x.elements.GetExpression(i);
  1946. IF expression IS SyntaxTree.MathArrayExpression THEN
  1947. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1948. ELSE
  1949. position := expression.position;
  1950. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1951. x.elements.SetExpression(i,expression);
  1952. isValue := isValue & (expression.resolved # NIL);
  1953. END;
  1954. END;
  1955. END RecursivelySetExpression;
  1956. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1957. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1958. arrayType: SyntaxTree.MathArrayType;
  1959. BEGIN
  1960. numberElements := x.elements.Length();
  1961. baseType := NIL;
  1962. gsize := 0;
  1963. FOR i := 0 TO numberElements-1 DO
  1964. expression := x.elements.GetExpression(i);
  1965. IF expression IS SyntaxTree.MathArrayExpression THEN
  1966. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1967. IF i=0 THEN
  1968. gsize := size;
  1969. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1970. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1971. ELSE expression.SetType(baseType)
  1972. END;
  1973. ELSIF baseType = NIL THEN baseType := type;
  1974. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1975. END;
  1976. END;
  1977. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1978. arrayType.SetArrayBase(baseType);
  1979. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1980. RETURN ResolveType(arrayType);
  1981. END RecursivelySetType;
  1982. BEGIN
  1983. type := NIL;
  1984. RecursivelyFindType(x);
  1985. isValue := TRUE;
  1986. RecursivelySetExpression(x);
  1987. arrayType := RecursivelySetType(x);
  1988. x.SetType(arrayType);
  1989. IF isValue THEN
  1990. value := SyntaxTree.NewMathArrayValue(x.position);
  1991. value.SetArray(x);
  1992. x.SetResolved(value);
  1993. value.SetType(arrayType);
  1994. END;
  1995. x.SetType(arrayType);
  1996. resolvedExpression := x;
  1997. END VisitMathArrayExpression;
  1998. (** check and resolve unary expression **)
  1999. PROCEDURE VisitUnaryExpression*(unaryExpression: SyntaxTree.UnaryExpression);
  2000. VAR
  2001. left: SyntaxTree.Expression;
  2002. int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; operator: LONGINT;
  2003. bool: BOOLEAN;
  2004. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  2005. value: SyntaxTree.Value;
  2006. BEGIN
  2007. type := SyntaxTree.invalidType;
  2008. left := ResolveExpression(unaryExpression.left);
  2009. unaryExpression.SetLeft(left);
  2010. operator := unaryExpression.operator;
  2011. result := unaryExpression;
  2012. IF ~system.operatorDefined[operator] THEN
  2013. Error(left.position,"Operator Not Defined");
  2014. RETURN
  2015. ELSIF left.type = NIL THEN
  2016. Error(left.position,"Invalid Nil Argument in Unary Expression");
  2017. resolvedExpression := SyntaxTree.invalidExpression;
  2018. RETURN
  2019. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2020. RETURN
  2021. END;
  2022. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2023. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2024. END;
  2025. IF operatorCall # NIL THEN
  2026. result := operatorCall;
  2027. type := operatorCall.type;
  2028. (* admissible operators
  2029. Minus number, set
  2030. Not boolean
  2031. *)
  2032. ELSE
  2033. CASE unaryExpression.operator OF
  2034. |Scanner.Minus:
  2035. IF IsIntegerType(left.type.resolved) THEN
  2036. IF left.resolved # NIL THEN
  2037. int := -left.resolved(SyntaxTree.IntegerValue).value;
  2038. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2039. result.SetResolved(value);
  2040. type := Global.GetIntegerType(system,int);
  2041. value.SetType(type);
  2042. ELSE
  2043. type := left.type
  2044. END
  2045. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2046. IF IsRealValue(left,real) THEN
  2047. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2048. result.SetResolved(value);
  2049. type := left.type;
  2050. value.SetType(type);
  2051. ELSE
  2052. type := left.type;
  2053. END;
  2054. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2055. IF IsSetValue(left,set) THEN
  2056. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2057. result.SetResolved(value);
  2058. type := Global.GetSetType(system,-set);
  2059. value.SetType(type);
  2060. ELSE
  2061. type := left.type;
  2062. END;
  2063. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2064. IF IsComplexValue(left, real, imaginary) THEN
  2065. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2066. result.SetResolved(value);
  2067. type := left.type;
  2068. value.SetType(type);
  2069. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2070. ELSE
  2071. type := left.type;
  2072. END
  2073. ELSE
  2074. Error(left.position,"unary operator not applicable");
  2075. END;
  2076. |Scanner.Not:
  2077. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2078. IF IsBooleanValue(left,bool) THEN
  2079. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2080. result.SetResolved(value);
  2081. type := system.booleanType;
  2082. value.SetType(type);
  2083. ELSE
  2084. type := system.booleanType;
  2085. END;
  2086. ELSE
  2087. Error(left.position,"unary operator not applicable");
  2088. END;
  2089. |Scanner.Plus:
  2090. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2091. result := left; type := left.type;
  2092. ELSE
  2093. Error(left.position,"unary operator not applicable");
  2094. END;
  2095. (* ADDRESS OF *)
  2096. |Scanner.Address:
  2097. IF HasAddress(left) THEN
  2098. type := system.addressType;
  2099. ELSE
  2100. type := SyntaxTree.invalidType;
  2101. Error(left.position,"has no address");
  2102. Printout.Info("par", left);
  2103. END;
  2104. (* SIZE OF *)
  2105. |Scanner.Size:
  2106. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2107. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2108. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2109. value := SyntaxTree.NewIntegerValue(left.position, int);
  2110. result.SetResolved(value);
  2111. type := Global.GetIntegerType(system,int);
  2112. value.SetType(type)
  2113. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2114. ELSE
  2115. (* for variables, system sizeof could represent the physically occupied size
  2116. determined via the type descriptor, implement that ? *)
  2117. Error(left.position,"is not a type symbol");
  2118. END
  2119. (* ALIAS OF *)
  2120. |Scanner.Alias:
  2121. type := left.type.resolved;
  2122. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2123. type := SyntaxTree.invalidType;
  2124. Error(left.position,"alias on non math array type");
  2125. END;
  2126. ELSE
  2127. Error(left.position,"unary operator not defined");
  2128. END;
  2129. END;
  2130. result.SetType(type);
  2131. resolvedExpression := result
  2132. END VisitUnaryExpression;
  2133. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2134. VAR
  2135. result: SyntaxTree.Expression;
  2136. array: SyntaxTree.MathArrayExpression;
  2137. value: SyntaxTree.MathArrayValue;
  2138. isValue: BOOLEAN;
  2139. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2140. BEGIN
  2141. type := type.resolved;
  2142. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2143. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2144. END;
  2145. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2146. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2147. END;
  2148. RETURN type
  2149. END BaseType;
  2150. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2151. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2152. BEGIN
  2153. numberElements := x.elements.Length();
  2154. FOR i := 0 TO numberElements-1 DO
  2155. expression := x.elements.GetExpression(i);
  2156. IF expression IS SyntaxTree.MathArrayExpression THEN
  2157. array := SyntaxTree.NewMathArrayExpression(position);
  2158. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2159. expression := array;
  2160. ELSE
  2161. position := expression.position;
  2162. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2163. isValue := isValue & (expression.resolved # NIL);
  2164. END;
  2165. to.elements.AddExpression(expression);
  2166. END;
  2167. END RecursivelyConvert;
  2168. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2169. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2170. arrayType: SyntaxTree.MathArrayType;
  2171. BEGIN
  2172. numberElements := x.elements.Length();
  2173. baseType := NIL;
  2174. gsize := 0;
  2175. FOR i := 0 TO numberElements-1 DO
  2176. expression := x.elements.GetExpression(i);
  2177. IF expression IS SyntaxTree.MathArrayExpression THEN
  2178. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2179. IF i=0 THEN
  2180. gsize := size;
  2181. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2182. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2183. ELSE expression.SetType(baseType)
  2184. END;
  2185. ELSIF baseType = NIL THEN baseType := type;
  2186. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2187. END;
  2188. END;
  2189. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2190. arrayType.SetArrayBase(baseType);
  2191. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2192. RETURN ResolveType(arrayType);
  2193. END RecursivelySetType;
  2194. BEGIN
  2195. result := SyntaxTree.invalidExpression;
  2196. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2197. result := expression (* do not convert *)
  2198. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2199. isValue := TRUE;
  2200. type := BaseType(type);
  2201. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2202. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2203. value := SyntaxTree.NewMathArrayValue(array.position);
  2204. value.SetArray(array);
  2205. value.SetType(RecursivelySetType(array));
  2206. result := value;
  2207. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2208. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2209. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2210. IF result = NIL THEN
  2211. result := SyntaxTree.invalidExpression;
  2212. Error(position, "incompatible conversion");
  2213. IF VerboseErrorMessage THEN
  2214. Printout.Info("expression",expression);
  2215. Printout.Info("type",type);
  2216. END;
  2217. END;
  2218. END;
  2219. RETURN result
  2220. END MathArrayConversion;
  2221. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2222. VAR result: SyntaxTree.Expression; int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; char: CHAR; string: Scanner.StringType;
  2223. BEGIN
  2224. result := expression; type := type.resolved;
  2225. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2226. (* skip, no conversion *)
  2227. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2228. int := expression(SyntaxTree.IntegerValue).value;
  2229. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2230. int := Global.ConvertSigned(int,system.SizeOf(type));
  2231. result := SyntaxTree.NewIntegerValue(position,int);
  2232. result.SetType(type);
  2233. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2234. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2235. result := SyntaxTree.NewIntegerValue(position,int);
  2236. result.SetType(type);
  2237. ELSIF (type IS SyntaxTree.FloatType) THEN
  2238. result := SyntaxTree.NewRealValue(expression.position,int);
  2239. result.SetType(type);
  2240. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2241. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2242. result.SetType(type);
  2243. ELSIF (type IS SyntaxTree.SetType) THEN
  2244. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,int));
  2245. result.SetType(type);
  2246. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2247. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2248. result.SetType(type);
  2249. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2250. result := SyntaxTree.NewEnumerationValue(expression.position,int);
  2251. result.SetType(type);
  2252. ELSIF (type IS SyntaxTree.PortType) THEN
  2253. result := ConvertValue(position, expression, system.integerType);
  2254. ELSE
  2255. Error(position, "integer value cannot be converted");
  2256. result := SyntaxTree.invalidExpression;
  2257. IF VerboseErrorMessage THEN
  2258. Printout.Info("expression",expression);
  2259. Printout.Info("type",type);
  2260. END;
  2261. END;
  2262. ELSIF IsRealValue(expression,real) THEN
  2263. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2264. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2265. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2266. result.SetType(type);
  2267. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2268. int := ENTIERH(real);
  2269. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2270. result.SetType(type);
  2271. ELSIF (type IS SyntaxTree.FloatType) THEN
  2272. result := SyntaxTree.NewRealValue(position,real);
  2273. result.SetType(type);
  2274. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2275. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2276. result.SetType(type);
  2277. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2278. ELSIF (type IS SyntaxTree.PortType) THEN
  2279. result := ConvertValue(position, expression, system.integerType);
  2280. ELSE
  2281. Error(position, "real value cannot be converted");
  2282. result := SyntaxTree.invalidExpression;
  2283. END
  2284. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2285. IF (type IS SyntaxTree.ComplexType) THEN
  2286. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2287. result.SetType(type);
  2288. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2289. ELSE
  2290. Error(position, "complex value cannot be converted");
  2291. result := SyntaxTree.invalidExpression;
  2292. END
  2293. ELSIF IsSetValue(expression,set) THEN
  2294. IF (type IS SyntaxTree.IntegerType) THEN
  2295. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,set));
  2296. result.SetType(type);
  2297. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(Basic.Integer,set)) *)
  2298. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2299. result.SetType(type);
  2300. ELSIF (type IS SyntaxTree.PortType) THEN
  2301. result := ConvertValue(position, expression, system.integerType);
  2302. ELSIF (type IS SyntaxTree.SetType) THEN
  2303. set := Global.ConvertSet(set,system.SizeOf(type));
  2304. result := SyntaxTree.NewSetValue(expression.position,set);
  2305. result.SetType(type);
  2306. ELSE
  2307. Error(position, "set value cannot be converted");
  2308. result := SyntaxTree.invalidExpression;
  2309. END;
  2310. ELSIF IsStringValue(expression,string) THEN
  2311. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2312. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2313. result.SetType(type);
  2314. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2315. ELSE
  2316. Error(position, "string value cannot be converted");
  2317. result := SyntaxTree.invalidExpression;
  2318. END;
  2319. ELSIF IsCharacterValue(expression,char) THEN
  2320. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2321. string[0] := char; string[1] := 0X;
  2322. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2323. result := SyntaxTree.NewStringValue(expression.position,string);
  2324. result.SetType(type);
  2325. ELSIF (type IS SyntaxTree.ByteType) THEN
  2326. (* do not simply set the new type as this could invalidate types of constants *)
  2327. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2328. result.SetType(type)
  2329. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2330. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,char));
  2331. result.SetType(type);
  2332. ELSIF (type IS SyntaxTree.SetType) THEN
  2333. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,Basic.Integer(ORD(char))));
  2334. result.SetType(type);
  2335. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2336. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2337. result.SetType(type);
  2338. ELSIF (type IS SyntaxTree.PortType) THEN
  2339. result := ConvertValue(position, expression, system.integerType);
  2340. ELSE
  2341. Error(position, "character value cannot be converted");
  2342. result := SyntaxTree.invalidExpression;
  2343. END;
  2344. ELSIF expression IS SyntaxTree.NilValue THEN
  2345. IF type IS SyntaxTree.AddressType THEN
  2346. result := SyntaxTree.NewIntegerValue(position,0);
  2347. result.SetType(type);
  2348. ELSE
  2349. result := expression;
  2350. END;
  2351. (* nothing to be done *)
  2352. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2353. result := MathArrayConversion(position, expression,type);
  2354. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2355. int := expression(SyntaxTree.EnumerationValue).value;
  2356. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2357. int := Global.ConvertSigned(int,system.SizeOf(type));
  2358. result := SyntaxTree.NewIntegerValue(position,int);
  2359. result.SetType(type);
  2360. ELSE
  2361. result := expression;
  2362. END;
  2363. (* nothing to be done *)
  2364. ELSE
  2365. Error(position, "expression cannot be converted");
  2366. IF VerboseErrorMessage THEN
  2367. Printout.Info("expression",expression);
  2368. Printout.Info("type",type);
  2369. END;
  2370. result := SyntaxTree.invalidExpression;
  2371. END;
  2372. RETURN result
  2373. END ConvertValue;
  2374. (**
  2375. return a conversion of an expression to a given type
  2376. - if expression is already of same type then return expression
  2377. - if incompatible conversion then report error and return invalidExpression
  2378. **)
  2379. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2380. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2381. BEGIN
  2382. type := type.resolved;
  2383. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2384. result := expression;
  2385. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2386. ELSIF expression = NIL THEN (* NIL expression *)
  2387. ELSIF expression.type = NIL THEN
  2388. Error(position, "expression of type NIL cannot be converted");
  2389. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2390. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2391. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2392. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2393. ELSIF expression.resolved # NIL THEN (* value *)
  2394. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2395. IF value IS SyntaxTree.Value THEN
  2396. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2397. result.SetResolved(value(SyntaxTree.Value));
  2398. result.SetType(value.type);
  2399. ELSE
  2400. result := value
  2401. END;
  2402. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2403. expressionList := SyntaxTree.NewExpressionList();
  2404. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2405. typeDeclaration.SetDeclaredType(type);
  2406. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2407. typeSymbol.SetType(typeDeclaration.type);
  2408. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2409. expressionList.AddExpression(expression);
  2410. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2411. result.SetType(type);
  2412. ELSIF IsArrayStructuredObjectType(type) THEN
  2413. (* no type can be converted to an array-structured object type *)
  2414. HALT(100)
  2415. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2416. IF inConversion>5 THEN
  2417. Error(expression.position,"recursive Conversion");
  2418. IF VerboseErrorMessage THEN
  2419. Printout.Info("expression",expression);
  2420. Printout.Info("type",type);
  2421. END;
  2422. END;
  2423. INC(inConversion);
  2424. IF inConversion < 10 THEN
  2425. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2426. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  2427. result := expression
  2428. ELSE
  2429. result := MathArrayConversion(position, expression,type);
  2430. END;
  2431. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2432. expression := ConvertToMathArray(expression);
  2433. type := MathArrayStructureOfType(type);
  2434. result := MathArrayConversion(position, expression, type)
  2435. ELSE
  2436. Error(expression.position,"cannot convert non array type to array type")
  2437. END;
  2438. END;
  2439. DEC(inConversion);
  2440. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2441. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  2442. result := expression; result.SetType(type);
  2443. ELSIF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2444. OR ~(type IS SyntaxTree.ArrayType) THEN
  2445. Error(expression.position,"cannot convert array type to non-array type")
  2446. END;
  2447. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2448. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2449. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2450. (*skip, no conversion*)
  2451. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2452. (* skip, no conversion *)
  2453. ELSE
  2454. ASSERT(~(type IS SyntaxTree.RangeType));
  2455. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2456. ASSERT(type # NIL);
  2457. END;
  2458. RETURN result
  2459. END NewConversion;
  2460. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2461. BEGIN
  2462. IF CompatibleTo(system,expression.type, type) THEN
  2463. RETURN NewConversion(position, expression, type, NIL);
  2464. ELSE
  2465. Error(expression.position, "incompatible expression");
  2466. RETURN SyntaxTree.invalidExpression
  2467. END;
  2468. END CompatibleConversion;
  2469. (**
  2470. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2471. **)
  2472. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2473. VAR leftType,rightType: SyntaxTree.Type;
  2474. BEGIN
  2475. IF left.type = NIL THEN Error(left.position,"no type")
  2476. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2477. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2478. (* error already handled *)
  2479. ELSE
  2480. leftType := left.type.resolved; rightType := right.type.resolved;
  2481. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2482. right := NewConversion(right.position, right, leftType, NIL);
  2483. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2484. left := NewConversion(left.position,left,rightType,NIL);
  2485. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2486. right := NewConversion(right.position, right, leftType, NIL);
  2487. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2488. left := NewConversion(left.position,left,rightType,NIL);
  2489. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2490. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2491. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2492. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2493. ELSIF
  2494. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2495. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2496. (* must be the case LONGREAL / COMPLEX ) *)
  2497. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2498. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2499. ELSE
  2500. Error(left.position,"incompatible operands");
  2501. END;
  2502. END;
  2503. END ConvertOperands;
  2504. (** find and return best operator matching to parameter list (nil, if none)
  2505. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2506. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2507. **)
  2508. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2509. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2510. identifier: SyntaxTree.Identifier;
  2511. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2512. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2513. BEGIN
  2514. operator := scope.firstOperator;
  2515. WHILE(operator # NIL) DO
  2516. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2517. procedureType := operator.type(SyntaxTree.ProcedureType);
  2518. distance := Distance(system, procedureType,actualParameters);
  2519. IF (distance < Infinity) THEN
  2520. IF returnType # NIL THEN
  2521. IF procedureType.returnType = NIL THEN
  2522. distance := Infinity
  2523. ELSE
  2524. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2525. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2526. END;
  2527. END;
  2528. END;
  2529. (*
  2530. IF distance < Infinity THEN
  2531. TRACE(distance, operator);
  2532. Printout.Info("potential operator",operator);
  2533. ELSE
  2534. Printout.Info("operator not possible",operator);
  2535. END;
  2536. *)
  2537. IF distance < bestDistance THEN
  2538. bestDistance := distance;
  2539. bestOperator := operator;
  2540. END;
  2541. END;
  2542. operator := operator.nextOperator;
  2543. END;
  2544. (*
  2545. Printout.Info("taken operator",bestOperator);
  2546. *)
  2547. END FindInScope;
  2548. BEGIN
  2549. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2550. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2551. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2552. import := currentScope.ownerModule.moduleScope.firstImport;
  2553. WHILE (bestDistance > 0) & (import # NIL) DO
  2554. IF import.module # NIL THEN
  2555. identifier := Global.GetIdentifier(operator,import.module.case);
  2556. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2557. END;
  2558. import := import.nextImport;
  2559. END;
  2560. RETURN bestOperator
  2561. END FindOperator;
  2562. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2563. BEGIN
  2564. currentScope := scope;
  2565. END SetCurrentScope;
  2566. (**
  2567. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2568. - handle LEN and DIM operator for array-structured object types
  2569. - find operator, if found then
  2570. - if in other module then add import designator
  2571. - create symbol designator for operator
  2572. - if error then return invalidExpression, if no operator then return NIL
  2573. **)
  2574. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2575. VAR
  2576. operator: SyntaxTree.Operator;
  2577. import: SyntaxTree.Import;
  2578. expression, result: SyntaxTree.Expression;
  2579. designator: SyntaxTree.Designator;
  2580. actualParameters, tempList: SyntaxTree.ExpressionList;
  2581. recordType: SyntaxTree.RecordType;
  2582. castReturnType : SyntaxTree.MathArrayType;
  2583. BEGIN
  2584. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2585. result := SyntaxTree.invalidExpression
  2586. ELSIF leftExpression = NIL THEN
  2587. result := NIL
  2588. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2589. (* LEN or DIM operator on array-structured object type *)
  2590. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2591. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2592. IF recordType.arrayAccessOperators.len = NIL THEN
  2593. Error(position, "call of undeclared LEN operator");
  2594. result := SyntaxTree.invalidExpression
  2595. ELSE
  2596. ASSERT(leftExpression IS SyntaxTree.Designator);
  2597. designator := leftExpression(SyntaxTree.Designator);
  2598. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2599. ASSERT(expression IS SyntaxTree.Designator);
  2600. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2601. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2602. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2603. result := designator
  2604. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2605. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2606. tempList := SyntaxTree.NewExpressionList();
  2607. tempList.AddExpression(rightExpression);
  2608. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2609. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2610. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2611. tempList := SyntaxTree.NewExpressionList();
  2612. tempList.AddExpression(designator);
  2613. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2614. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2615. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2616. END
  2617. END;
  2618. ELSE
  2619. IF ~complexNumbersImported THEN
  2620. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2621. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2622. THEN
  2623. (* operators on complex numbers *)
  2624. ImportModule(Global.ComplexNumbersName,position);
  2625. complexNumbersImported := TRUE;
  2626. END;
  2627. END;
  2628. (* import OCArrayBase if needed *)
  2629. IF ~arrayBaseImported THEN
  2630. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2631. IF op = Global.Dim THEN
  2632. (* not existing in OCArrayBase *)
  2633. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2634. (* not existing in OCArrayBase *)
  2635. ELSE
  2636. ImportModule(Global.ArrayBaseName,position);
  2637. arrayBaseImported := TRUE;
  2638. END
  2639. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2640. ImportModule(Global.ArrayBaseName,position);
  2641. arrayBaseImported := TRUE
  2642. END;
  2643. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2644. (* LEN(RANGE) *)
  2645. ImportModule(Global.ArrayBaseName,position);
  2646. arrayBaseImported := TRUE;
  2647. END;
  2648. END;
  2649. actualParameters := SyntaxTree.NewExpressionList();
  2650. actualParameters.AddExpression(leftExpression);
  2651. IF rightExpression # NIL THEN
  2652. actualParameters.AddExpression(rightExpression)
  2653. END;
  2654. operator := FindOperator(system,op,actualParameters,resultType);
  2655. IF operator # NIL THEN
  2656. designator := NIL;
  2657. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2658. import := currentScope.ownerModule.moduleScope.firstImport;
  2659. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2660. import := import.nextImport;
  2661. END;
  2662. expression := NewSymbolDesignator(position,NIL,import);
  2663. designator := expression(SyntaxTree.Designator);
  2664. END;
  2665. expression := NewSymbolDesignator(position,designator,operator);
  2666. designator := expression(SyntaxTree.Designator);
  2667. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2668. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2669. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2670. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2671. result.SetType(castReturnType);
  2672. END;
  2673. ELSE
  2674. result := NIL;
  2675. END;
  2676. END;
  2677. RETURN result
  2678. END NewOperatorCall;
  2679. (** check and resolve binary expression **)
  2680. (*! clean up *)
  2681. PROCEDURE VisitBinaryExpression*(binaryExpression: SyntaxTree.BinaryExpression);
  2682. VAR left,right,result: SyntaxTree.Expression;
  2683. leftType, rightType: SyntaxTree.Type;
  2684. 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;
  2685. cl,cr: CHAR;
  2686. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2687. type: SyntaxTree.Type;
  2688. value: SyntaxTree.Value;
  2689. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: Basic.Integer;
  2690. integerConstantFolding: BOOLEAN;
  2691. list: SyntaxTree.ExpressionList;
  2692. PROCEDURE NewBool(v: BOOLEAN);
  2693. BEGIN
  2694. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2695. value.SetType(system.booleanType);
  2696. result.SetResolved(value);
  2697. type := system.booleanType
  2698. END NewBool;
  2699. PROCEDURE NewSet(v: Basic.Set);
  2700. BEGIN
  2701. value := Global.NewSetValue(system,binaryExpression.position,v);
  2702. result.SetResolved(value);
  2703. type := value.type;
  2704. END NewSet;
  2705. PROCEDURE NewInteger(v: Basic.Integer; t: SyntaxTree.Type);
  2706. BEGIN
  2707. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2708. (* type cast to "larger" type only if the value is still in the range *)
  2709. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2710. value.SetType(t);
  2711. END;
  2712. result.SetResolved(value);
  2713. type := value.type;
  2714. END NewInteger;
  2715. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2716. BEGIN
  2717. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2718. value.SetType(t);
  2719. result.SetResolved(value);
  2720. type := t;
  2721. END NewReal;
  2722. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2723. BEGIN
  2724. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2725. value.SetType(t);
  2726. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2727. result.SetResolved(value);
  2728. type := t;
  2729. END NewComplex;
  2730. BEGIN
  2731. type := SyntaxTree.invalidType;
  2732. left := ResolveExpression(binaryExpression.left);
  2733. right := ResolveExpression(binaryExpression.right);
  2734. binaryExpression.SetLeft(left);
  2735. binaryExpression.SetRight(right);
  2736. result := binaryExpression;
  2737. operator := binaryExpression.operator;
  2738. IF ~system.operatorDefined[operator] THEN
  2739. Error(left.position,"Operator Not Defined");
  2740. result := SyntaxTree.invalidExpression;
  2741. RETURN
  2742. END;
  2743. IF left.type = NIL THEN
  2744. Error(left.position,"Expression has no result type");
  2745. result := SyntaxTree.invalidExpression;
  2746. RETURN;
  2747. END;
  2748. IF right.type = NIL THEN
  2749. Error(right.position,"Expression has no result type");
  2750. result := SyntaxTree.invalidExpression;
  2751. RETURN;
  2752. END;
  2753. leftType := left.type.resolved; rightType := right.type.resolved;
  2754. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2755. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2756. END;
  2757. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2758. list := SyntaxTree.NewExpressionList();
  2759. list.AddExpression(right);
  2760. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2761. END;
  2762. IF operatorCall # NIL THEN
  2763. result := operatorCall;
  2764. type := operatorCall.type;
  2765. (* admissible operators:
  2766. Times, Plus, Minus numeric numeric numeric
  2767. set set set
  2768. Slash numeric numeric real /complex
  2769. set set set
  2770. Div , Mod integer integer integer
  2771. And, Or bool bool bool
  2772. Equal, Unequal basic basic bool
  2773. pointer pointer bool
  2774. object object bool
  2775. record record bool
  2776. string string bool
  2777. enumerator enumerator bool
  2778. Less, LessEqual,
  2779. Greater, GreaterEqual integer/real integer/real bool
  2780. enumerator enumerator bool
  2781. In integer set bool
  2782. Is pointer type bool
  2783. object type bool
  2784. record type bool
  2785. Upto: special abbreviation for a..b
  2786. *)
  2787. ELSIF (left.type = NIL) THEN
  2788. Error(left.position,"type (left operand) = NIL in binary expression");
  2789. D.Str("nil type in "); D.Type(left); D.Ln;
  2790. result := SyntaxTree.invalidExpression;
  2791. ELSIF (right.type = NIL) THEN
  2792. Error(right.position,"type (right operand) = NIL in binary expression");
  2793. result := SyntaxTree.invalidExpression;
  2794. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2795. result := SyntaxTree.invalidExpression;
  2796. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2797. HALT(100);
  2798. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2799. type := system.booleanType;
  2800. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2801. Error(right.position,"is not a type ");
  2802. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2803. Error(binaryExpression.position,"is not a type extension of ");
  2804. IF VerboseErrorMessage THEN
  2805. Printout.Info("left",left);
  2806. Printout.Info("right",right);
  2807. END;
  2808. ELSIF IsUnsafePointer(left.type) THEN
  2809. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2810. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2811. NewBool(TRUE)
  2812. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2813. NewBool(TRUE);
  2814. ELSIF IsUnextensibleRecord(left) THEN
  2815. NewBool(FALSE)
  2816. END
  2817. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2818. Error(right.position,"must not be a type");
  2819. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2820. Error(left.position,"must not be a type");
  2821. ELSIF operator = Scanner.In THEN (* left IN right *)
  2822. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2823. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2824. NewBool(il IN sr);
  2825. ELSE
  2826. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2827. left := NewConversion(left.position, left, system.longintType,NIL);
  2828. binaryExpression.SetLeft(left)
  2829. END;
  2830. type := system.booleanType;
  2831. END
  2832. ELSE
  2833. Error(binaryExpression.position, "incompatible operands");
  2834. END
  2835. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2836. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2837. Error(binaryExpression.position,"incompatible operands");
  2838. END;
  2839. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2840. ELSE Error(binaryExpression.position,"operator not defined 1")
  2841. END
  2842. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2843. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2844. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2845. Error(binaryExpression.position,"incompatible operands");
  2846. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2847. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2848. left := NewConversion(left.position, left, system.addressType, NIL);
  2849. right := NewConversion(right.position, right, system.addressType, NIL);
  2850. binaryExpression.SetLeft(left);
  2851. binaryExpression.SetRight(right);
  2852. type := system.addressType;
  2853. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2854. ConvertOperands(left, right);
  2855. binaryExpression.SetLeft(left);
  2856. binaryExpression.SetRight(right);
  2857. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2858. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2859. END;
  2860. type := system.booleanType;
  2861. ELSE
  2862. Error(binaryExpression.position,"operator not defined 3");
  2863. END
  2864. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2865. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2866. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2867. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2868. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2869. CASE operator OF
  2870. |Scanner.Equal: NewBool(strl^=strr^);
  2871. |Scanner.Unequal:NewBool(strl^#strr^);
  2872. |Scanner.Less: NewBool(strl^<strr^);
  2873. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2874. |Scanner.Greater: NewBool(strl^>strr^);
  2875. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2876. ELSE
  2877. Error(binaryExpression.position,"operator not defined 4");
  2878. END;
  2879. END;
  2880. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2881. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2882. type := system.booleanType
  2883. ELSE
  2884. Error(binaryExpression.position,"operator not defined 5");
  2885. END;
  2886. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2887. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2888. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2889. type := system.booleanType;
  2890. ELSE
  2891. Error(binaryExpression.position,"operator not defined 6");
  2892. END
  2893. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2894. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2895. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2896. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2897. type := system.booleanType
  2898. ELSE
  2899. Error(binaryExpression.position,"operator not defined for enumerators");
  2900. END;
  2901. ELSE
  2902. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2903. END;
  2904. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2905. type := system.booleanType;
  2906. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2907. type := system.booleanType;
  2908. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2909. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2910. THEN
  2911. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2912. IF (leftType # rightType) THEN
  2913. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2914. ConvertOperands(left,right); (* operands must be of the same type here *)
  2915. END;
  2916. binaryExpression.SetLeft(left);
  2917. binaryExpression.SetRight(right);
  2918. leftType := left.type.resolved;
  2919. rightType := right.type.resolved;
  2920. END;
  2921. type := leftType;
  2922. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2923. Error(binaryExpression.position,"conversion failed ?");
  2924. IF VerboseErrorMessage THEN
  2925. Printout.Info("left",left);
  2926. Printout.Info("right",right);
  2927. END;
  2928. ELSIF IsIntegerType(leftType) THEN
  2929. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2930. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2931. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2932. Error(binaryExpression.position,"division by zero");
  2933. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2934. Error(binaryExpression.position,"integer division by negative number");
  2935. END;
  2936. END;
  2937. (* constant folding *)
  2938. (* bootstrap64
  2939. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2940. CASE operator OF
  2941. |Scanner.Plus: NewInteger(il+ir,left.type);
  2942. |Scanner.Minus: NewInteger(il-ir,left.type);
  2943. |Scanner.Times: NewInteger(il*ir,left.type);
  2944. |Scanner.Slash:
  2945. IF ir # 0 THEN
  2946. NewReal(il/ir, system.realType);
  2947. END;
  2948. |Scanner.Mod:
  2949. IF ir > 0 THEN
  2950. NewInteger(il MOD ir,left.type);
  2951. END;
  2952. |Scanner.Div:
  2953. IF ir > 0 THEN
  2954. NewInteger(il DIV ir,left.type);
  2955. END;
  2956. |Scanner.Equal: NewBool(il=ir);
  2957. |Scanner.Unequal:NewBool(il#ir);
  2958. |Scanner.Less: NewBool(il<ir);
  2959. |Scanner.LessEqual: NewBool(il<=ir);
  2960. |Scanner.Greater: NewBool(il>ir);
  2961. |Scanner.GreaterEqual: NewBool(il>=ir);
  2962. ELSE Error(binaryExpression.position,"operator not defined 7");
  2963. END;
  2964. ELS*)
  2965. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2966. hl := left.resolved(SyntaxTree.IntegerValue).value;
  2967. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2968. CASE operator OF
  2969. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2970. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2971. |Scanner.Times: NewInteger(hl*hr,left.type);
  2972. |Scanner.Slash:
  2973. IF hr = 0 THEN
  2974. Error(binaryExpression.position,"division by zero");
  2975. ELSE
  2976. IF type.sizeInBits = 64 THEN
  2977. NewReal(hl/hr,system.longrealType);
  2978. ELSE
  2979. NewReal(hl/hr,system.realType)
  2980. END
  2981. END;
  2982. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2983. |Scanner.Mod:
  2984. IF hr = 0 THEN
  2985. Error(binaryExpression.position,"division by zero");
  2986. ELSE
  2987. NewInteger(hl MOD hr, left.type);
  2988. (* bootstrap64
  2989. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2990. *)
  2991. END;
  2992. |Scanner.Div:
  2993. IF hr = 0 THEN
  2994. Error(binaryExpression.position,"division by zero");
  2995. ELSE
  2996. NewInteger(hl DIV hr, left.type);
  2997. (* bootstrap64
  2998. NewInteger(Machine.DivH(hl,hr),left.type);
  2999. *)
  3000. END;
  3001. (* *)
  3002. |Scanner.Equal: NewBool(hl=hr);
  3003. |Scanner.Unequal: NewBool(hl#hr);
  3004. |Scanner.Less: NewBool(hl<hr);
  3005. |Scanner.LessEqual: NewBool(hl<=hr);
  3006. |Scanner.Greater: NewBool(hl>hr);
  3007. |Scanner.GreaterEqual:NewBool(hl>=hr);
  3008. ELSE Error(binaryExpression.position,"operator not defined 8");
  3009. END;
  3010. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  3011. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  3012. type := left.type
  3013. ELSIF (operator = Scanner.Slash) THEN
  3014. left := NewConversion(left.position,left,system.realType,NIL);
  3015. right := NewConversion(right.position,right,system.realType,NIL);
  3016. binaryExpression.SetLeft(left);
  3017. binaryExpression.SetRight(right);
  3018. type := system.realType
  3019. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3020. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3021. type := system.booleanType
  3022. ELSE
  3023. Error(binaryExpression.position,"operator not defined 9");
  3024. END;
  3025. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  3026. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  3027. CASE operator OF
  3028. |Scanner.Plus: NewReal(rl+rr,leftType);
  3029. |Scanner.Minus: NewReal(rl-rr,leftType);
  3030. |Scanner.Times:NewReal(rl*rr,leftType);
  3031. |Scanner.Slash:
  3032. IF rr = 0 THEN
  3033. Error(binaryExpression.position,"division by zero");
  3034. ELSE
  3035. NewReal(rl/rr,leftType);
  3036. END
  3037. |Scanner.Equal: NewBool(rl=rr);
  3038. |Scanner.Unequal: NewBool(rl#rr);
  3039. |Scanner.Less: NewBool(rl<rr);
  3040. |Scanner.LessEqual: NewBool(rl<=rr);
  3041. |Scanner.Greater: NewBool(rl>rr);
  3042. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3043. ELSE Error(binaryExpression.position,"operator not defined 10");
  3044. END;
  3045. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3046. type := left.type
  3047. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3048. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3049. type := system.booleanType
  3050. ELSE
  3051. Error(binaryExpression.position,"operator not defined 11");
  3052. IF VerboseErrorMessage THEN
  3053. Printout.Info("left",left);
  3054. Printout.Info("right",right);
  3055. END;
  3056. END;
  3057. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3058. CASE operator OF
  3059. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3060. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3061. ELSE
  3062. Error(binaryExpression.position,"operator not defined");
  3063. IF VerboseErrorMessage THEN
  3064. Printout.Info("left", left);
  3065. Printout.Info("right", right)
  3066. END;
  3067. END;
  3068. IF ~error THEN
  3069. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3070. Error(binaryExpression.position,"division by zero")
  3071. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3072. (* do constant folding *)
  3073. CASE operator OF
  3074. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3075. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3076. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3077. |Scanner.Slash:
  3078. divisor := c * c + d * d;
  3079. ASSERT(divisor # 0);
  3080. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3081. |Scanner.Equal: NewBool((a = c) & (b = d))
  3082. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3083. END
  3084. END
  3085. END
  3086. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3087. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3088. CASE operator OF
  3089. |Scanner.And: NewBool(bl & br);
  3090. |Scanner.Or: NewBool(bl OR br);
  3091. |Scanner.Equal: NewBool(bl = br);
  3092. |Scanner.Unequal: NewBool(bl # br);
  3093. ELSE Error(binaryExpression.position,"operator not defined 12");
  3094. END;
  3095. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3096. type := system.booleanType
  3097. ELSE
  3098. Error(binaryExpression.position,"operator not defined 13");
  3099. END;
  3100. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3101. (* constant folding *)
  3102. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3103. IF operator = Scanner.Equal THEN
  3104. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3105. ELSIF operator = Scanner.Unequal THEN
  3106. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3107. END;
  3108. END;
  3109. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3110. type := system.booleanType;
  3111. ELSE
  3112. Error(binaryExpression.position, "operator not defined");
  3113. END;
  3114. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3115. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3116. CASE operator OF
  3117. |Scanner.Plus: NewSet(sl + sr);
  3118. |Scanner.Minus: NewSet(sl - sr);
  3119. |Scanner.Times: NewSet(sl * sr);
  3120. |Scanner.Slash: NewSet(sl / sr);
  3121. |Scanner.Equal: NewBool(sl=sr);
  3122. |Scanner.Unequal: NewBool(sl#sr);
  3123. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3124. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3125. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3126. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3127. ELSE Error(binaryExpression.position,"operator not defined 14");
  3128. END;
  3129. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3130. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3131. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3132. THEN
  3133. type := system.booleanType
  3134. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3135. type := left.type
  3136. ELSE
  3137. Error(binaryExpression.position,"operator not defined 15");
  3138. END;
  3139. ELSIF IsCharacterType(left.type) THEN
  3140. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3141. CASE operator OF
  3142. |Scanner.Equal: NewBool(cl=cr);
  3143. |Scanner.Unequal: NewBool(cl#cr);
  3144. |Scanner.Less: NewBool(cl<cr);
  3145. |Scanner.LessEqual: NewBool(cl<=cr);
  3146. |Scanner.Greater: NewBool(cl>cr);
  3147. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3148. ELSE Error(binaryExpression.position,"operator not defined 16");
  3149. END;
  3150. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3151. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3152. type := system.booleanType
  3153. ELSE
  3154. Error(binaryExpression.position,"operator not defined 17");
  3155. END;
  3156. ELSE
  3157. Error(binaryExpression.position,"operator not defined 18");
  3158. END;
  3159. ELSE
  3160. Error(binaryExpression.position,"operator not defined 19");
  3161. END;
  3162. IF type = SyntaxTree.invalidType THEN
  3163. result := SyntaxTree.invalidExpression
  3164. ELSE
  3165. result.SetType(type)
  3166. END;
  3167. resolvedExpression := result
  3168. END VisitBinaryExpression;
  3169. (** resolve a range expression of the from <<first .. last BY step>>
  3170. - depending on the context different things are checked:
  3171. ArrayIndex:
  3172. - components must be integers
  3173. - replace missing lower bound with 0
  3174. - replace missing upper bound with MAX(LONGINT)
  3175. - replace missing step size with 1
  3176. SetElement:
  3177. - components must be integers
  3178. - replace missing lower bound with 0
  3179. - replace missing upper bound with MAX(SET)
  3180. - must not have step size
  3181. CaseGuard:
  3182. - components must be constant
  3183. - components must be integers or characters
  3184. - must have lower and upper bound present
  3185. - components are made compatible
  3186. - must not have step size
  3187. - if error: return invalidExpression
  3188. **)
  3189. PROCEDURE VisitRangeExpression*(x: SyntaxTree.RangeExpression);
  3190. VAR
  3191. hasError: BOOLEAN;
  3192. first, last, step: SyntaxTree.Expression;
  3193. BEGIN
  3194. hasError := FALSE;
  3195. first := x.first;
  3196. last := x.last;
  3197. step := x.step;
  3198. (* check lower bound *)
  3199. IF x.context = SyntaxTree.CaseGuard THEN
  3200. IF first = NIL THEN
  3201. Error(x.position, "missing lower bound");
  3202. hasError := TRUE
  3203. ELSE
  3204. first := ResolveExpression(first);
  3205. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3206. Error(first.position, "lower bound not integer or character");
  3207. hasError := TRUE
  3208. ELSE
  3209. IF first IS SyntaxTree.StringValue THEN
  3210. (* add conversion from string to character *)
  3211. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3212. END
  3213. END;
  3214. (* check if expression is constant *)
  3215. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3216. (* error already reported *)
  3217. hasError := TRUE
  3218. END
  3219. END
  3220. ELSE (* ArrayIndex, SetElement *)
  3221. IF first = NIL THEN
  3222. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3223. END;
  3224. first := ResolveExpression(first);
  3225. IF IsIntegerType(first.type.resolved) THEN
  3226. first := NewConversion(first.position, first, system.lenType, NIL)
  3227. ELSE
  3228. Error(first.position, "lower bound not integer");
  3229. hasError := TRUE
  3230. END
  3231. END;
  3232. (* check upper bound *)
  3233. IF x.context = SyntaxTree.CaseGuard THEN
  3234. IF last = NIL THEN
  3235. Error(x.position, "missing upper bound");
  3236. hasError := TRUE
  3237. ELSE
  3238. last := ResolveExpression(last);
  3239. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3240. Error(last.position, "lower bound not integer or character");
  3241. hasError := TRUE
  3242. ELSE
  3243. IF last IS SyntaxTree.StringValue THEN
  3244. (* add conversion from string to character *)
  3245. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3246. END
  3247. END;
  3248. (* check if expression is constant *)
  3249. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3250. (* error already reported *)
  3251. hasError := TRUE
  3252. ELSE
  3253. (* try to make lower and upper bound compatible *)
  3254. ConvertOperands(first, last);
  3255. IF first.type.resolved # last.type.resolved THEN
  3256. Error(x.position, "lower and upper bounds incompatible");
  3257. hasError := TRUE
  3258. END
  3259. END
  3260. END
  3261. ELSE (* ArrayIndex, SetElement *)
  3262. IF last = NIL THEN
  3263. IF x.context = SyntaxTree.ArrayIndex THEN
  3264. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3265. ELSE
  3266. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3267. END
  3268. END;
  3269. last := ResolveExpression(last);
  3270. IF IsIntegerType(last.type.resolved) THEN
  3271. last := NewConversion(last.position, last, system.lenType, NIL)
  3272. ELSE
  3273. Error(last.position, "upper bound not integer");
  3274. hasError := TRUE
  3275. END
  3276. END;
  3277. (* check step size *)
  3278. IF x.context = SyntaxTree.ArrayIndex THEN
  3279. IF step = NIL THEN
  3280. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3281. END;
  3282. step := ResolveExpression(step);
  3283. IF IsIntegerType(step.type.resolved) THEN
  3284. step := NewConversion(step.position, step, system.lenType, NIL)
  3285. ELSE
  3286. Error(step.position, "step size not integer");
  3287. hasError := TRUE
  3288. END
  3289. ELSE (* SetElement, CaseGuard *)
  3290. IF step # NIL THEN
  3291. Error(last.position, "step size not allowed in this context");
  3292. hasError := TRUE
  3293. END
  3294. END;
  3295. IF hasError THEN
  3296. resolvedExpression := SyntaxTree.invalidExpression
  3297. ELSE
  3298. x.SetFirst(first);
  3299. x.SetLast(last);
  3300. x.SetStep(step);
  3301. x.SetType(system.rangeType);
  3302. resolvedExpression := x;
  3303. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3304. END
  3305. END VisitRangeExpression;
  3306. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3307. BEGIN
  3308. x.SetType(NIL);
  3309. resolvedExpression := x;
  3310. END VisitTensorRangeExpression;
  3311. (** resolve the expression d and return result as designator
  3312. - resolve expression
  3313. - if expression is a designator then return designator else error message and return invalidDesignator
  3314. **)
  3315. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3316. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3317. BEGIN
  3318. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3319. resolved := ResolveExpression(d);
  3320. IF resolved = SyntaxTree.invalidExpression THEN
  3321. (* error should already have been reported *)
  3322. result := SyntaxTree.invalidDesignator;
  3323. ELSIF resolved IS SyntaxTree.Designator THEN
  3324. result := resolved(SyntaxTree.Designator);
  3325. ELSE
  3326. Error(d.position,"is no designator ! ");
  3327. result := SyntaxTree.invalidDesignator;
  3328. END;
  3329. (* result.type might be nil. *)
  3330. RETURN result
  3331. END ResolveDesignator;
  3332. (**
  3333. symbol designator generated in this module
  3334. nothing to be resolved
  3335. **)
  3336. PROCEDURE VisitSymbolDesignator*(x: SyntaxTree.SymbolDesignator);
  3337. BEGIN
  3338. resolvedExpression := x;
  3339. END VisitSymbolDesignator;
  3340. (**
  3341. self designator generated in this module
  3342. nothing to be resolved
  3343. **)
  3344. PROCEDURE VisitSelfDesignator*(x: SyntaxTree.SelfDesignator);
  3345. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3346. BEGIN
  3347. (* check if in record scope *)
  3348. scope := currentScope;
  3349. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3350. scope := scope.outerScope;
  3351. END;
  3352. IF scope = NIL THEN (* in module scope *)
  3353. x.SetType(system.anyType);
  3354. ELSIF scope IS SyntaxTree.CellScope THEN
  3355. cell := scope(SyntaxTree.CellScope).ownerCell;
  3356. x.SetType(cell);
  3357. ELSE (* in record scope *)
  3358. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3359. IF (record # NIL) & (record.pointerType # NIL) THEN
  3360. type := ResolveType(record.pointerType);
  3361. x.SetType(type);
  3362. ELSE
  3363. x.SetType(record);
  3364. END;
  3365. END;
  3366. resolvedExpression := x;
  3367. END VisitSelfDesignator;
  3368. PROCEDURE VisitResultDesignator*(x: SyntaxTree.ResultDesignator);
  3369. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3370. BEGIN
  3371. scope := currentScope;
  3372. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3373. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3374. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3375. returnType := procedureType.returnType;
  3376. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3377. THEN
  3378. x.SetType(returnType);
  3379. ELSE
  3380. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3381. x.SetType(SyntaxTree.invalidType);
  3382. END;
  3383. ELSE
  3384. Error(x.position,"forbidden access to result designator");
  3385. x.SetType(SyntaxTree.invalidType);
  3386. END;
  3387. x.SetAssignable(TRUE);
  3388. resolvedExpression := x;
  3389. END VisitResultDesignator;
  3390. (**
  3391. return symbol designator as an expression
  3392. - if symbol is a constant then return the constant value expression
  3393. - else
  3394. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3395. - if symbol is a guarded variable then return a TypeGuardDesignator
  3396. - else return a symbol designator
  3397. **)
  3398. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3399. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3400. guardType: SyntaxTree.Type;
  3401. BEGIN
  3402. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3403. result := SyntaxTree.invalidExpression;
  3404. ASSERT(symbol # NIL);
  3405. (*
  3406. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3407. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3408. Error(position, "type not allowed here");
  3409. ELS *)
  3410. (* not needed any more as values are stored in the expression
  3411. IF symbol IS SyntaxTree.Constant THEN
  3412. result := symbol(SyntaxTree.Constant).value
  3413. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3414. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3415. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3416. ELSE
  3417. result := symbol(SyntaxTree.Constant).value
  3418. END;
  3419. ELSE
  3420. *)
  3421. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3422. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3423. THEN
  3424. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3425. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3426. left := NewDereferenceDesignator(position,left);
  3427. left.SetHidden(TRUE);
  3428. END;
  3429. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3430. scope := currentScope;
  3431. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3432. scope := scope.outerScope;
  3433. END;
  3434. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3435. Error(position, "forbidden access to symbol in parent procedure scope");
  3436. END;
  3437. END;
  3438. 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);
  3439. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3440. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3441. ELSE
  3442. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3443. END;
  3444. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3445. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3446. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3447. result.SetType(symbol.type);
  3448. result.SetAssignable(assignable);
  3449. symbol.MarkUsed;
  3450. IF symbol IS SyntaxTree.Constant THEN
  3451. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3452. END;
  3453. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3454. variableAccessed := TRUE
  3455. END;
  3456. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3457. IF GetGuard(symbol,guardType) THEN
  3458. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  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. expression.Accept(SELF);
  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. x.Accept(SELF);
  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 procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6454. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6455. IF useDarwinCCalls THEN (*fld*)
  6456. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6457. ELSE
  6458. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6459. END
  6460. END;
  6461. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6462. procedureType.SetInterrupt(TRUE);
  6463. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6464. END;
  6465. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6466. procedureType.SetNoReturn(TRUE);
  6467. END;
  6468. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  6469. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6470. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6471. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6472. END;
  6473. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6474. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6475. END;
  6476. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6477. IF ~PowerOf2(value) THEN
  6478. Error(position, "forbidden stack alignment - must be power of two");
  6479. ELSE
  6480. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6481. END;
  6482. END;
  6483. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6484. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6485. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6486. END;
  6487. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6488. SyntaxTree.InitFingerPrint(fp);
  6489. fp.shallow := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6490. fp.public := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6491. fp.private := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6492. fp.shallowAvailable := TRUE;
  6493. procedure.SetFingerPrint(fp);
  6494. END;
  6495. CheckModifiers(modifiers, TRUE);
  6496. modifiers := procedureType.returnTypeModifiers;
  6497. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6498. CheckModifiers(modifiers, TRUE);
  6499. procedure.SetState(SyntaxTree.Resolved);
  6500. FixProcedureType(procedureType);
  6501. currentIsRealtime := procedureType.isRealtime;
  6502. currentIsBodyProcedure := procedure.isBodyProcedure;
  6503. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6504. THEN
  6505. Error(procedure.position,"problems during parameter offset computation");
  6506. END;
  6507. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6508. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6509. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6510. Error(procedure.position,"problem during parameter offset generation");
  6511. END;
  6512. END;
  6513. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6514. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6515. procedureType.SetDelegate(TRUE);
  6516. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6517. (* add auto-self *)
  6518. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6519. IF (record.pointerType.typeDeclaration = NIL) THEN
  6520. selfParameter.SetType(record.pointerType);
  6521. ELSE
  6522. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6523. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6524. qualifiedType.SetResolved(record.pointerType);
  6525. selfParameter.SetType(qualifiedType);
  6526. END;
  6527. selfParameter.SetAccess(SyntaxTree.Hidden);
  6528. END;
  6529. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6530. IF procedure.isConstructor THEN
  6531. (*! constructor is always visible, compatibility to paco
  6532. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6533. *)
  6534. procedure.MarkUsed;
  6535. IF procedureType.returnType # NIL THEN
  6536. Error(procedure.position,"constructor with forbidden return type");
  6537. END;
  6538. proc := procedure.scope.firstProcedure;
  6539. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6540. proc := proc.nextProcedure;
  6541. END;
  6542. IF proc # NIL THEN
  6543. Error(procedure.position,"duplicate constructor")
  6544. ELSE
  6545. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6546. END;
  6547. END;
  6548. IF procedure.isFinalizer THEN
  6549. procedure.MarkUsed;
  6550. IF procedureType.returnType # NIL THEN
  6551. Error(procedure.position,"finalizer with forbidden return type");
  6552. END;
  6553. IF procedureType.numberParameters # 0 THEN
  6554. Error(procedure.position,"finalizer with formal parameters");
  6555. END;
  6556. proc := procedure.scope.firstProcedure;
  6557. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6558. proc := proc.nextProcedure;
  6559. END;
  6560. IF proc # NIL THEN
  6561. Error(procedure.position,"duplicate finalizer")
  6562. ELSE
  6563. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6564. END;
  6565. END;
  6566. super := FindSuperProcedure(record.recordScope, procedure);
  6567. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6568. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6569. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6570. END;
  6571. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6572. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6573. END;
  6574. IF super.isFinal THEN
  6575. Error(procedure.position,"forbidden method extending final method");
  6576. END;
  6577. (*
  6578. IF super.access # procedure.access THEN
  6579. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6580. END;
  6581. *)
  6582. procedure.SetSuper(super);
  6583. super.SetOverwritten(TRUE);
  6584. procedure.SetAccess(procedure.access+super.access);
  6585. procedure.MarkUsed;
  6586. END;
  6587. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6588. THEN
  6589. Error(procedure.position,"problems during parameter offset computation");
  6590. END;
  6591. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6592. IF cellsAreObjects THEN
  6593. procedureType.SetDelegate(TRUE);
  6594. END;
  6595. IF procedure.isConstructor THEN
  6596. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6597. END;
  6598. ELSIF procedure.isConstructor THEN
  6599. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6600. END;
  6601. Declarations(procedure.procedureScope, FALSE, {0,1});
  6602. (* body resolution part done as late fix of the procedure type *)
  6603. procedure.SetState(SyntaxTree.Resolved);
  6604. currentIsRealtime := recentIsRealtime;
  6605. currentIsBodyProcedure := recentIsBodyProcedure;
  6606. END;
  6607. END VisitProcedure;
  6608. (**
  6609. a builtin procedure is a global item that may not be modified locally
  6610. instead the resolving of builtin procedure calls are done in the esignator
  6611. **)
  6612. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6613. VAR type: SyntaxTree.Type;
  6614. BEGIN
  6615. type := ResolveType(builtinProcedure.type);
  6616. END VisitBuiltin;
  6617. (* nopov *)
  6618. (** check and resolve operator
  6619. - operators are first checked as procedures
  6620. - then additional operator-specific checks are done
  6621. - note that only module-scope operators are checked here
  6622. (operators in a record scope are only allowed in the context of
  6623. array-structured object types and checked in 'ResolveArrayStructure')
  6624. - also note that inter-operator conformity is not checked here
  6625. **)
  6626. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6627. VAR
  6628. procedureType: SyntaxTree.ProcedureType;
  6629. leftType, rightType: SyntaxTree.Type;
  6630. identifierNumber: LONGINT; position: Position;
  6631. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6632. modifiers: SyntaxTree.Modifier;
  6633. (** whether a type is locally defined in the current module scope
  6634. for arrays, the base type must be locally defined **)
  6635. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6636. BEGIN
  6637. IF type = NIL THEN
  6638. RETURN FALSE
  6639. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6640. RETURN TRUE
  6641. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6642. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6643. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6644. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6645. ELSE
  6646. RETURN FALSE
  6647. END
  6648. END IsLocallyDefined;
  6649. BEGIN
  6650. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6651. procedureType := operator.type(SyntaxTree.ProcedureType);
  6652. modifiers := procedureType.modifiers;
  6653. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6654. CheckModifiers(modifiers, TRUE);
  6655. VisitProcedure(operator);
  6656. IF operator.scope IS SyntaxTree.RecordScope THEN
  6657. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6658. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6659. IF identifierNumber = -1 THEN
  6660. Error(operator.position, "operator with unknown identifier")
  6661. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6662. Error(operator.position, "identifier may not be used for operator")
  6663. ELSE
  6664. IF procedureType.numberParameters < 1 THEN
  6665. Error(operator.position, "operator without operand");
  6666. ELSIF procedureType.numberParameters > 2 THEN
  6667. Error(operator.position, "operator with more than two operands");
  6668. ELSE
  6669. (* determine operand types *)
  6670. leftType := procedureType.firstParameter.type;
  6671. IF procedureType.numberParameters > 1 THEN
  6672. rightType := procedureType.firstParameter.nextParameter.type
  6673. ELSE
  6674. rightType := NIL
  6675. END;
  6676. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6677. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6678. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6679. Error(operator.position, "none of the operands is declared in the same module")
  6680. END
  6681. END;
  6682. (* TODO: refine the checks, think about how restrictive the checks should be
  6683. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6684. They might be used for intersection, union, complement of custom object types *)
  6685. (* defaults *)
  6686. hasReturnType := TRUE;
  6687. mustBeUnary := FALSE;
  6688. mustBeBinary := FALSE;
  6689. mustReturnBoolean := FALSE;
  6690. mustReturnInteger := FALSE;
  6691. mustHaveEquitypedOperands := FALSE;
  6692. (* operator-specific exceptions *)
  6693. CASE identifierNumber OF
  6694. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6695. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6696. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6697. mustBeBinary := TRUE
  6698. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6699. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6700. | Scanner.Times: mustBeBinary := TRUE
  6701. | Scanner.TimesTimes: mustBeBinary := TRUE
  6702. | Scanner.DotTimes: mustBeBinary := TRUE
  6703. | Scanner.PlusTimes: mustBeBinary := TRUE
  6704. | Scanner.Slash: mustBeBinary := TRUE
  6705. | Scanner.Backslash: mustBeBinary := TRUE
  6706. | Scanner.DotSlash: mustBeBinary := TRUE
  6707. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6708. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6709. | Scanner.Not: mustBeUnary := TRUE
  6710. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6711. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6712. | Scanner.Transpose: mustBeUnary := TRUE;
  6713. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6714. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6715. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6716. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6717. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6718. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6719. | Global.Abs: mustBeUnary := TRUE;
  6720. | Global.Ash: (* TODO: arity? *)
  6721. | Global.Cap: (* TODO: arity? *)
  6722. | Global.Chr: mustBeUnary := TRUE;
  6723. | Global.Entier: (* TODO: arity? *)
  6724. | Global.EntierH: (* TODO: arity? *)
  6725. | Global.Len: (* unary and binary *)
  6726. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6727. | Global.Max, Global.Min: (* unary and binary *)
  6728. | Global.Odd: (* TODO: arity? *)
  6729. | Global.Sum: (* TODO: arity? *)
  6730. | Global.All: (* TODO: arity? *)
  6731. | Global.Re, Global.Im:
  6732. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6733. | Scanner.Alias:
  6734. | Scanner.GreaterGreater, Scanner.LessLess:
  6735. mustBeBinary := TRUE; hasReturnType := FALSE;
  6736. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6737. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6738. END;
  6739. (* check parameter count *)
  6740. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6741. Error(operator.position,"operator is not unary")
  6742. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6743. Error(operator.position,"operator is not binary")
  6744. END;
  6745. (* check parameter types *)
  6746. (* TODO: is this used at all? *)
  6747. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6748. leftType := procedureType.firstParameter.type;
  6749. rightType := procedureType.firstParameter.nextParameter.type;
  6750. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6751. Error(operator.position, "the two operands are not of the same type")
  6752. END
  6753. END;
  6754. (* check return type *)
  6755. IF hasReturnType THEN
  6756. IF procedureType.returnType = NIL THEN
  6757. Error(operator.position, "return type required")
  6758. ELSIF mustReturnBoolean THEN
  6759. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6760. Error(operator.position,"return type is not Boolean")
  6761. END
  6762. ELSIF mustReturnInteger THEN
  6763. IF ~IsSizeType(procedureType.returnType, system.addressSize) THEN
  6764. Error(operator.position,"return type is no size type")
  6765. END
  6766. END
  6767. ELSIF procedureType.returnType # NIL THEN
  6768. Error(operator.position, "return type not allowed")
  6769. END
  6770. END
  6771. END
  6772. END
  6773. END VisitOperator;
  6774. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6775. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6776. BEGIN
  6777. IF error THEN RETURN FALSE END;
  6778. prevScope := currentScope;
  6779. prevDiagnostics := diagnostics;
  6780. diagnostics := NIL; (* suppress error output *)
  6781. currentScope := module.moduleScope;
  6782. VisitImport(x);
  6783. IF ~error THEN
  6784. module.moduleScope.AddImport(x);
  6785. x.SetScope(module.moduleScope);
  6786. END;
  6787. currentScope := prevScope;
  6788. diagnostics := prevDiagnostics;
  6789. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6790. END AddImport;
  6791. (** check and resolve import
  6792. - check for name = SYSTEM
  6793. - check for forbidden self import
  6794. - search through global import cache: already imported?
  6795. - check if already imported indirectly
  6796. - import if necessary -> set module and enter into import cache
  6797. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6798. - after this import this direct import and all indirect imports are stored in the current module's import list
  6799. **)
  6800. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6801. VAR
  6802. module: SyntaxTree.Module;
  6803. moduleScope: SyntaxTree.ModuleScope;
  6804. import,reimport: SyntaxTree.Import;
  6805. filename: FileName;
  6806. prevScope: SyntaxTree.Scope;
  6807. BEGIN
  6808. IF SymbolNeedsResolution(x) THEN
  6809. prevScope := currentScope;
  6810. x.SetType(SyntaxTree.importType);
  6811. moduleScope := currentScope.ownerModule.moduleScope;
  6812. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6813. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6814. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6815. Error(x.position,"forbidden self import");
  6816. ELSE
  6817. (* search through global import list: already imported ? *)
  6818. IF (x.module = NIL) & (importCache # NIL) THEN
  6819. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6820. ELSE import := NIL
  6821. END;
  6822. IF x.module # NIL THEN (* already imported indirectly *)
  6823. module := x.module;
  6824. ELSIF import # NIL THEN (* already in module list *)
  6825. module := import.module;
  6826. ASSERT(module # NIL);
  6827. x.SetModule(module);
  6828. ELSE (* must be imported *)
  6829. Global.ModuleFileName(x.moduleName,x.context,filename);
  6830. IF symbolFileFormat # NIL THEN
  6831. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6832. IF module = NIL THEN
  6833. ErrorSS(x.position,"could not import",filename);
  6834. IF VerboseErrorMessage THEN
  6835. Printout.Info("import",x)
  6836. END
  6837. ELSE
  6838. (*
  6839. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6840. (*! should rather be done by importer *)
  6841. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6842. checker.importCache := importCache;
  6843. checker.arrayBaseImported := arrayBaseImported;
  6844. checker.global := global;
  6845. checker.Module(module); (* semantic check *)
  6846. error := error OR checker.error;
  6847. END;
  6848. *)
  6849. (*
  6850. ASSERT(SyntaxTree.Resolved IN module.state);
  6851. *)
  6852. x.SetModule(module);
  6853. IF importCache # NIL THEN
  6854. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6855. import.SetContext(x.context);
  6856. import.SetModule(module);
  6857. importCache.AddImport(import);
  6858. END;
  6859. END;
  6860. ELSE
  6861. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6862. END;
  6863. END;
  6864. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6865. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6866. END;
  6867. import := module.moduleScope.firstImport;
  6868. WHILE(import # NIL) DO
  6869. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6870. ASSERT(currentScope # NIL);
  6871. ASSERT(currentScope.ownerModule # NIL);
  6872. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6873. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6874. Error(x.position,"recursive import");
  6875. ELSE
  6876. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6877. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6878. IF reimport = NIL THEN (* indirect import *)
  6879. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6880. reimport.SetContext(import.context);
  6881. reimport.SetModule(import.module);
  6882. moduleScope.AddImport(reimport);
  6883. reimport.SetScope(moduleScope);
  6884. ELSE
  6885. ASSERT(import.module # NIL);
  6886. reimport.SetModule(import.module); (* direct or indirect import *)
  6887. END;
  6888. END;
  6889. import := import.nextImport;
  6890. END;
  6891. END;
  6892. END;
  6893. currentScope := prevScope;
  6894. (* ELSE nothing to be done *)
  6895. x.SetState(SyntaxTree.Resolved);
  6896. END;
  6897. END VisitImport;
  6898. (*** statements ***)
  6899. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6900. VAR prev,resolved: SyntaxTree.Statement;
  6901. BEGIN
  6902. prev := resolvedStatement;
  6903. resolvedStatement := x;
  6904. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6905. activeCellsStatement := FALSE;
  6906. x.Accept(SELF);
  6907. (* removed this, implementation restriction should be resolved by backend
  6908. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6909. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6910. END;
  6911. *)
  6912. resolved := resolvedStatement;
  6913. resolvedStatement := prev;
  6914. RETURN resolved
  6915. END ResolveStatement;
  6916. (** check and resolve statement sequence
  6917. - check all statements, replace if necessary
  6918. **)
  6919. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6920. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6921. BEGIN
  6922. IF statementSequence # NIL THEN (* else empty *)
  6923. FOR i := 0 TO statementSequence.Length()-1 DO
  6924. statement := statementSequence.GetStatement(i);
  6925. resolved := ResolveStatement(statement);
  6926. IF (resolved # statement) THEN
  6927. statementSequence.SetStatement(i,resolved);
  6928. END;
  6929. END;
  6930. END;
  6931. END StatementSequence;
  6932. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6933. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6934. - check if procedure is callable
  6935. - check return type = NIL (otherwise must be assignment statement)
  6936. **)
  6937. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6938. VAR call: SyntaxTree.Designator;
  6939. BEGIN
  6940. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6941. call := procedureCall.call;
  6942. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6943. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6944. END;
  6945. call := ResolveDesignator(call);
  6946. IF call = SyntaxTree.invalidDesignator THEN
  6947. (* error already handled *)
  6948. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6949. (* inline call in a statement *)
  6950. ELSIF ~IsCallable(call) THEN
  6951. Error(procedureCall.position,"called object is not a procedure");
  6952. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6953. Error(procedureCall.position,"calling procedure with non-void return type");
  6954. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6955. END;
  6956. procedureCall.SetCall(call);
  6957. (*
  6958. IF call = SyntaxTree.invalidDesignator THEN
  6959. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6960. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6961. IF IsOberonInline(procedure) THEN
  6962. Warning(procedure.position,"call to inline proc");
  6963. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6964. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6965. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6966. resolvedStatement := block;
  6967. RETURN;
  6968. END;
  6969. END;
  6970. *)
  6971. END VisitProcedureCallStatement;
  6972. (** check and resolve assignment LHS := RHS
  6973. - resolve LHS and RHS
  6974. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6975. - check if assignment is compatible
  6976. - check if LHS is variable (i.e. assignable)
  6977. - convert RHS if necessary
  6978. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6979. - assignment between different ASOTs
  6980. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6981. - assignment to ASOT elements:
  6982. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6983. **)
  6984. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6985. VAR
  6986. left: SyntaxTree.Designator;
  6987. right, expression: SyntaxTree.Expression;
  6988. designator: SyntaxTree.Designator;
  6989. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6990. mathArrayType: SyntaxTree.MathArrayType;
  6991. BEGIN
  6992. right := ResolveExpression(assignment.right);
  6993. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6994. left := ResolveDesignator(assignment.left);
  6995. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6996. (* error already handled *)
  6997. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6998. (* LHS is index write operator call on ASOT *)
  6999. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  7000. (* necessary ?
  7001. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  7002. type := procedureType.firstParameter.type;
  7003. expression := procedureCallDesignator.parameters.GetExpression(0);
  7004. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  7005. *)
  7006. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7007. ELSIF CheckVariable(left) THEN
  7008. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  7009. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7010. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7011. (* conversion done by procedure call
  7012. (* try to convert to left argument *)
  7013. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7014. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7015. procedureCallDesignator.parameters.SetExpression(1, right);
  7016. END;
  7017. *)
  7018. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7019. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  7020. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  7021. ELSIF AssignmentCompatible(left, right) THEN
  7022. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  7023. mathArrayType := MathArrayStructureOfType(left.type);
  7024. right := NewConversion(right.position, right, mathArrayType, NIL);
  7025. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  7026. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  7027. ELSE
  7028. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7029. assignment.SetLeft(left);
  7030. assignment.SetRight(right);
  7031. resolvedStatement := assignment
  7032. END
  7033. END
  7034. END
  7035. END VisitAssignment;
  7036. (** check and resolve assignment LHS := RHS
  7037. - resolve LHS and RHS
  7038. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7039. - check if assignment is compatible
  7040. - check if LHS is variable (i.e. assignable)
  7041. - convert RHS if necessary
  7042. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7043. - assignment between different ASOTs
  7044. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7045. - assignment to ASOT elements:
  7046. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7047. **)
  7048. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7049. VAR
  7050. left: SyntaxTree.Designator;
  7051. right: SyntaxTree.Expression;
  7052. inPort, outPort: SyntaxTree.PortType;
  7053. expression: SyntaxTree.Expression;
  7054. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7055. BEGIN
  7056. right := ResolveExpression(communication.right);
  7057. left := ResolveDesignator(communication.left);
  7058. communication.SetLeft(left);
  7059. communication.SetRight(right);
  7060. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7061. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7062. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7063. (* conversion done by procedure call
  7064. (* try to convert to left argument *)
  7065. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7066. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7067. procedureCallDesignator.parameters.SetExpression(1, right);
  7068. END;
  7069. *)
  7070. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7071. ELSE
  7072. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7073. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7074. (* error already handled *)
  7075. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7076. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7077. IF outPort.direction # SyntaxTree.OutPort THEN
  7078. Error(left.position,"not an out-port")
  7079. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7080. Error(left.position,"incompatible to port type");
  7081. ELSE
  7082. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7083. communication.SetRight(right)
  7084. END;
  7085. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7086. IF CheckVariable(left) THEN
  7087. IF inPort.direction # SyntaxTree.InPort THEN
  7088. Error(left.position,"not an in-port")
  7089. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7090. Error(right.position,"incompatible to port type");
  7091. END;
  7092. END;
  7093. ELSE
  7094. Error(communication.position,"unsupported stream operation");
  7095. END;
  7096. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7097. IF outPort.direction # SyntaxTree.OutPort THEN
  7098. Error(left.position,"not an out-port")
  7099. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7100. Error(left.position,"incompatible to port type");
  7101. ELSE
  7102. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7103. communication.SetRight(right)
  7104. END;
  7105. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7106. IF CheckVariable(right) THEN
  7107. IF inPort.direction # SyntaxTree.InPort THEN
  7108. Error(left.position,"not an in-port")
  7109. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7110. Error(right.position,"incompatible to port type");
  7111. END;
  7112. END;
  7113. ELSE
  7114. Error(communication.position, "unsupported operation");
  7115. END;
  7116. END;
  7117. END VisitCommunicationStatement;
  7118. (** check and resolve if/eslif part
  7119. - check condition
  7120. - check statement sequence
  7121. **)
  7122. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7123. VAR prevUnreachable, b: BOOLEAN;
  7124. BEGIN
  7125. prevUnreachable := currentIsUnreachable;
  7126. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7127. IF IsBooleanValue(ifPart.condition,b) THEN
  7128. IF b=FALSE THEN
  7129. currentIsUnreachable := TRUE
  7130. ELSIF b=TRUE THEN
  7131. true := TRUE
  7132. END;
  7133. END;
  7134. StatementSequence(ifPart.statements);
  7135. currentIsUnreachable := prevUnreachable;
  7136. END IfPart;
  7137. (** check and resolve if statement
  7138. - check if parts and else part statement sequence
  7139. **)
  7140. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7141. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7142. BEGIN
  7143. prevUnreachable := currentIsUnreachable;
  7144. ifPartTrue := FALSE;
  7145. IfPart(ifStatement.ifPart,ifPartTrue);
  7146. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7147. elsif := ifStatement.GetElsifPart(i);
  7148. IfPart(elsif,ifPartTrue);
  7149. END;
  7150. IF ifStatement.elsePart # NIL THEN
  7151. IF ifPartTrue THEN
  7152. currentIsUnreachable := TRUE
  7153. END;
  7154. StatementSequence(ifStatement.elsePart)
  7155. END;
  7156. currentIsUnreachable := prevUnreachable;
  7157. END VisitIfStatement;
  7158. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7159. VAR variable: SyntaxTree.Designator;
  7160. type,variableType: SyntaxTree.Type;
  7161. withEntry: WithEntry;
  7162. BEGIN
  7163. variable := ResolveDesignator(withPart.variable);
  7164. variableType := variable.type.resolved;
  7165. withPart.SetVariable(variable);
  7166. type := ResolveType(withPart.type);
  7167. withPart.SetType(type);
  7168. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7169. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7170. END;
  7171. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7172. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7173. Error(variable.position,"is not extensible designator");
  7174. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7175. Error(variable.position,"is no local variable ");
  7176. IF VerboseErrorMessage THEN
  7177. Printout.Info("variable",variable)
  7178. END;
  7179. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7180. Error(variable.position,"withguarded symbol is no type extension of ");
  7181. IF VerboseErrorMessage THEN
  7182. Printout.Info("variable",variable);
  7183. Printout.Info("type",type);
  7184. END;
  7185. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7186. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7187. Error(variable.position,"withguarded symbol is no variable ");
  7188. IF VerboseErrorMessage THEN
  7189. Printout.Info("variable",variable);
  7190. Printout.Info("type",type);
  7191. END;
  7192. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7193. Error(variable.position,"invalid change of withguarded symbol");
  7194. ELSE
  7195. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7196. NEW(withEntry);
  7197. withEntry.previous := withEntries;
  7198. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7199. withEntry.type := type;
  7200. withEntries := withEntry;
  7201. StatementSequence(withPart.statements);
  7202. withEntries := withEntries.previous;
  7203. END;
  7204. END WithPart;
  7205. (** check and resolve with statement WITH variable: type DO ... END;
  7206. - check type and variable
  7207. - check that variable type is type extension of type
  7208. - check that variable is a variable
  7209. - enter new with scope and enter guardedVariable with same name and reference to variable
  7210. - create if statement:
  7211. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7212. **)
  7213. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7214. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7215. BEGIN
  7216. prevScope := currentScope; symbol := NIL;
  7217. FOR i := 0 TO withStatement.WithParts()-1 DO
  7218. WithPart(withStatement.GetWithPart(i),symbol);
  7219. END;
  7220. IF withStatement.elsePart # NIL THEN
  7221. StatementSequence(withStatement.elsePart)
  7222. END;
  7223. currentScope := prevScope;
  7224. END VisitWithStatement;
  7225. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7226. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7227. - check 'first' < 'last' and no overlaps between different case labels
  7228. - check statement sequence
  7229. **)
  7230. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  7231. VAR
  7232. i: LONGINT;
  7233. position: Position;
  7234. expression, left, right: SyntaxTree.Expression;
  7235. expressionType: SyntaxTree.Type;
  7236. l, r: Basic.Integer;
  7237. cl, cr: CHAR;
  7238. thiscases: SyntaxTree.CaseConstant;
  7239. BEGIN
  7240. thiscases := NIL;
  7241. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7242. expression := casePart.elements.GetExpression(i);
  7243. position := expression.position;
  7244. (* set context of range *)
  7245. IF expression IS SyntaxTree.RangeExpression THEN
  7246. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7247. END;
  7248. expression := ResolveExpression(expression);
  7249. IF expression = SyntaxTree.invalidExpression THEN
  7250. (* error already reported *)
  7251. expressionType := SyntaxTree.invalidType;
  7252. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7253. (* read out 'first' and 'last' *)
  7254. left := expression(SyntaxTree.RangeExpression).first;
  7255. right := expression(SyntaxTree.RangeExpression).last;
  7256. (* guaranteed by VisitRangeExpression: *)
  7257. ASSERT((left # NIL) & (right # NIL));
  7258. ASSERT(left.type.resolved = right.type.resolved);
  7259. left := CompatibleConversion(left.position, left, type);
  7260. right := CompatibleConversion(right.position, right, type);
  7261. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7262. expression(SyntaxTree.RangeExpression).SetLast(right);
  7263. expressionType := RegularType(position,left.type);
  7264. ELSE
  7265. expression := ConstantExpression(expression);
  7266. expression := CompatibleConversion(expression.position, expression, type);
  7267. (*
  7268. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7269. left := Global.NewCharacterValue(system,expression.position,cl);
  7270. expression := casePart.elements.GetExpression(i);
  7271. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7272. expression := left
  7273. END;
  7274. *)
  7275. casePart.elements.SetExpression(i,expression);
  7276. left := expression; right := expression;
  7277. expressionType := RegularType(position,expression.type)
  7278. END;
  7279. IF (expressionType = SyntaxTree.invalidType) THEN
  7280. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7281. Error(position, "inadmissible case label");
  7282. expression := SyntaxTree.invalidExpression;
  7283. ELSE
  7284. l := 0; r := 0;
  7285. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7286. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7287. l := ORD(cl); r := ORD(cr);
  7288. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7289. ELSE
  7290. expression := SyntaxTree.invalidExpression
  7291. END;
  7292. IF expression # SyntaxTree.invalidExpression THEN
  7293. IF l>r THEN
  7294. Error(position, "empty case label")
  7295. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7296. Error(position, "duplicate case label");
  7297. ELSE
  7298. IF l < min THEN min := l END;
  7299. IF r > max THEN max := r END;
  7300. END;
  7301. END;
  7302. END;
  7303. casePart.elements.SetExpression(i,expression);
  7304. END;
  7305. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7306. casePart.SetConstants(thiscases);
  7307. StatementSequence(casePart.statements);
  7308. END CasePart;
  7309. (** check and resolve case statement CASE variable OF ... END;
  7310. - check variable
  7311. - check case parts
  7312. **)
  7313. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7314. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7315. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer; msg: ARRAY 64 OF CHAR;
  7316. BEGIN
  7317. expression := ResolveExpression(caseStatement.variable);
  7318. type := RegularType(expression.position,expression.type);
  7319. IF type = SyntaxTree.invalidType THEN
  7320. expression := SyntaxTree.invalidExpression;
  7321. ELSIF IsIntegerType(type) THEN
  7322. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7323. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7324. (*
  7325. expression := Global.NewCharacterValue(system,expression.position,ch);
  7326. *)
  7327. type := expression.type;
  7328. ELSIF IsCharacterType(type) THEN
  7329. ELSIF IsEnumerationType(type) THEN
  7330. ELSE
  7331. Error(caseStatement.variable.position,"variable must be integer or character type");
  7332. expression := SyntaxTree.invalidExpression;
  7333. END;
  7334. caseStatement.SetVariable(expression);
  7335. caseList := NIL;
  7336. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  7337. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7338. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7339. END;
  7340. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7341. msg := "huge sparse case table ";
  7342. Strings.AppendInt(msg, max-min);
  7343. Strings.Append(msg,"/");
  7344. Strings.AppendInt(msg, caseStatement.CaseParts());
  7345. Warning(caseStatement.position,msg);
  7346. END;
  7347. caseStatement.SetMinMax(min,max);
  7348. StatementSequence(caseStatement.elsePart);
  7349. IF expression.resolved # NIL THEN
  7350. IF IsCharacterValue(expression,ch) THEN
  7351. l := ORD(ch)
  7352. ELSIF IsIntegerValue(expression,l) THEN
  7353. END;
  7354. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7355. END;
  7356. END VisitCaseStatement;
  7357. (** check and resolve while statement
  7358. - check condition
  7359. - check statement sequence
  7360. **)
  7361. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7362. VAR prevIsUnreachable,b: BOOLEAN;
  7363. BEGIN
  7364. prevIsUnreachable := currentIsUnreachable;
  7365. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7366. IF IsBooleanValue(whileStatement.condition,b) THEN
  7367. IF b=FALSE THEN
  7368. currentIsUnreachable := TRUE
  7369. END;
  7370. END;
  7371. StatementSequence(whileStatement.statements);
  7372. currentIsUnreachable := prevIsUnreachable
  7373. END VisitWhileStatement;
  7374. (** check and resolve repeat statement
  7375. - check condition
  7376. - check statement sequence
  7377. **)
  7378. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7379. BEGIN
  7380. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7381. StatementSequence(repeatStatement.statements);
  7382. END VisitRepeatStatement;
  7383. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7384. VAR withEntry: WithEntry;
  7385. BEGIN
  7386. withEntry := withEntries;
  7387. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7388. withEntry := withEntry.previous
  7389. END;
  7390. IF withEntry = NIL THEN RETURN FALSE
  7391. ELSE
  7392. type := withEntry.type;
  7393. RETURN TRUE
  7394. END;
  7395. END GetGuard;
  7396. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7397. - check that variable is an integer variable
  7398. - check that from is integer typed with compatible type
  7399. - check that to has compatible type
  7400. - check that by is constant integer with compatible type
  7401. **)
  7402. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7403. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7404. BEGIN
  7405. designator := ResolveDesignator(forStatement.variable);
  7406. type := SyntaxTree.invalidType;
  7407. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7408. designator := SyntaxTree.invalidDesignator;
  7409. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7410. Error(designator.position,"control variable of non-integer type");
  7411. designator := SyntaxTree.invalidDesignator;
  7412. ELSIF CheckVariable(designator) THEN
  7413. type := designator.type;
  7414. END;
  7415. forStatement.SetVariable(designator);
  7416. expression := ResolveExpression(forStatement.from);
  7417. IF expression = SyntaxTree.invalidExpression THEN
  7418. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7419. Error(expression.position,"start value of incompatible type");
  7420. expression := SyntaxTree.invalidExpression;
  7421. ELSIF type # SyntaxTree.invalidType THEN
  7422. expression := NewConversion(expression.position,expression,type,NIL)
  7423. END;
  7424. forStatement.SetFrom(expression);
  7425. expression := ResolveExpression(forStatement.to);
  7426. IF expression = SyntaxTree.invalidExpression THEN
  7427. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7428. Error(expression.position,"end value of incompatible type");
  7429. expression := SyntaxTree.invalidExpression;
  7430. ELSIF type # SyntaxTree.invalidType THEN
  7431. expression := NewConversion(expression.position,expression,type,NIL)
  7432. END;
  7433. forStatement.SetTo(expression);
  7434. IF forStatement.by # NIL THEN
  7435. expression := ConstantInteger(forStatement.by);
  7436. ELSE
  7437. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7438. END;
  7439. IF expression = SyntaxTree.invalidExpression THEN
  7440. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7441. Error(expression.position,"step value of incompatible type");
  7442. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  7443. Error(expression.position,"invalid step value");
  7444. ELSIF type # SyntaxTree.invalidType THEN
  7445. expression := NewConversion(expression.position,expression,type,NIL)
  7446. END;
  7447. forStatement.SetBy(expression);
  7448. StatementSequence(forStatement.statements);
  7449. END VisitForStatement;
  7450. (** check and resolve loop statement LOOP StatementSequence END
  7451. - check statement sequence
  7452. **)
  7453. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7454. BEGIN
  7455. StatementSequence(loopStatement.statements)
  7456. END VisitLoopStatement;
  7457. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7458. BEGIN
  7459. StatementSequence(exitableBlock.statements);
  7460. END VisitExitableBlock;
  7461. (** check and resolve exit statement EXIT
  7462. - check that exit is within LOOP statement block
  7463. **)
  7464. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7465. VAR outer: SyntaxTree.Statement;
  7466. BEGIN
  7467. outer := exitStatement.outer;
  7468. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7469. outer := outer.outer;
  7470. END;
  7471. IF outer = NIL THEN
  7472. Error(exitStatement.position,"exit statement not within loop statement");
  7473. END;
  7474. END VisitExitStatement;
  7475. (** check and resolve return statement RETURN [expression]
  7476. - check expression (if any)
  7477. - check if in procedure scope
  7478. - if in procedure scope then check expression compatibility
  7479. - if not in procecdure scope then check on return without expression
  7480. **)
  7481. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7482. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7483. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7484. BEGIN
  7485. position := returnStatement.position;
  7486. expression := returnStatement.returnValue;
  7487. IF expression # NIL THEN
  7488. expression := ResolveExpression(expression);
  7489. returnStatement.SetReturnValue(expression);
  7490. END;
  7491. outer := returnStatement.outer;
  7492. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7493. outer := outer.outer
  7494. END;
  7495. IF (outer # NIL) THEN
  7496. scope := outer(SyntaxTree.Body).inScope;
  7497. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7498. IF (expression # NIL) THEN
  7499. Error(position, "return statement with parameter not in procedure scope");
  7500. END;
  7501. ELSE
  7502. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7503. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7504. Error(position, "return statement in procedure that does not return");
  7505. END;
  7506. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7507. IF returnType # NIL THEN
  7508. returnType := returnType.resolved;
  7509. IF expression = NIL THEN
  7510. Error(position, "empty return type in procedure providing a return type")
  7511. ELSIF expression.type = NIL THEN
  7512. Error(position,"returned type incompatible: expression has no type");
  7513. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7514. Error(position, "return type not compatible");
  7515. IF VerboseErrorMessage THEN
  7516. Printout.Info("returnType",returnType);
  7517. Printout.Info("expression",expression);
  7518. END;
  7519. ELSE
  7520. expression := NewConversion(expression.position,expression,returnType,NIL);
  7521. returnStatement.SetReturnValue(expression);
  7522. END;
  7523. ELSIF expression # NIL THEN
  7524. Error(position, "non-empty return type in procedure providing no return type");
  7525. END;
  7526. END;
  7527. END;
  7528. END VisitReturnStatement;
  7529. (** check and resolve await statement AWAIT(condition: Expression)
  7530. - check await condition
  7531. **)
  7532. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7533. VAR condition: SyntaxTree.Expression;
  7534. BEGIN
  7535. condition := ResolveCondition(awaitStatement.condition);
  7536. IF currentIsRealtime THEN
  7537. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7538. END;
  7539. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7540. Error(awaitStatement.position,"senseless await statement with constant condition");
  7541. END;
  7542. awaitStatement.SetCondition(condition);
  7543. END VisitAwaitStatement;
  7544. PROCEDURE CheckSystemImport(position: Position);
  7545. VAR import: SyntaxTree.Import;
  7546. BEGIN
  7547. import := currentScope.ownerModule.moduleScope.firstImport;
  7548. WHILE(import # NIL) DO
  7549. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7550. RETURN;
  7551. END;
  7552. import := import.nextImport;
  7553. END;
  7554. Error(position, "forbidden code without system import");
  7555. END CheckSystemImport;
  7556. (** check and resolve code statement: do nothing, must be done by assembler
  7557. **)
  7558. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7559. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7560. BEGIN
  7561. CheckSystemImport(code.position);
  7562. FOR i := 0 TO code.inRules.Length()-1 DO
  7563. statement := code.inRules.GetStatement(i);
  7564. IF statement IS SyntaxTree.Assignment THEN
  7565. WITH statement: SyntaxTree.Assignment DO
  7566. statement.SetRight(ResolveExpression(statement.right));
  7567. END;
  7568. ELSE
  7569. Error(statement.position, "can only be assignment")
  7570. END;
  7571. END;
  7572. FOR i := 0 TO code.outRules.Length()-1 DO
  7573. statement := code.outRules.GetStatement(i);
  7574. IF statement IS SyntaxTree.Assignment THEN
  7575. WITH statement: SyntaxTree.Assignment DO
  7576. statement.SetLeft(ResolveDesignator(statement.left));
  7577. END;
  7578. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7579. (* must be a reference to some register *)
  7580. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7581. ELSE
  7582. Printout.Info("out statement ", statement);
  7583. Error(statement.position, "(out) can only be assignment")
  7584. END;
  7585. END;
  7586. END VisitCode;
  7587. (** check and set flags of a statement block
  7588. - check for multiply occurence of a flag
  7589. - check and set priority only in bodies
  7590. - check for valid names
  7591. **)
  7592. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7593. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7594. flag: LONGINT; recordBody: SyntaxTree.Body;
  7595. PROCEDURE SetProtectedRecord;
  7596. VAR scope: SyntaxTree.Scope;
  7597. BEGIN
  7598. scope := currentScope;
  7599. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7600. scope := scope.outerScope
  7601. END;
  7602. IF scope # NIL THEN
  7603. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7604. END;
  7605. END SetProtectedRecord;
  7606. BEGIN
  7607. flags := {};
  7608. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7609. recordBody := block(SyntaxTree.Body)
  7610. ELSE
  7611. recordBody := NIL
  7612. END;
  7613. blockModifier := block.blockModifiers;
  7614. WHILE(blockModifier # NIL) DO
  7615. name := blockModifier.identifier;
  7616. expression := blockModifier.expression;
  7617. position := blockModifier.position;
  7618. flag := -1;
  7619. IF name=Global.NamePriority THEN
  7620. IF expression = NIL THEN
  7621. Error(position, "missing priority expression");
  7622. ELSIF recordBody = NIL THEN
  7623. Error(position, "priority not on record body");
  7624. ELSIF recordBody.priority # NIL THEN
  7625. Error(position, "duplicate priority expression");
  7626. ELSE
  7627. recordBody.SetPriority(expression);
  7628. END;
  7629. ELSIF expression # NIL THEN
  7630. Error(expression.position,"expression not in connection with priority")
  7631. ELSIF name=Global.NameExclusive THEN
  7632. IF block.isExclusive THEN
  7633. Error(position, "duplicate exclusive flag")
  7634. END;
  7635. block.SetExclusive(TRUE); SetProtectedRecord;
  7636. ELSIF name=Global.NameActive THEN
  7637. IF recordBody = NIL THEN
  7638. Error(position, "active not in record body");
  7639. ELSIF recordBody.isActive THEN
  7640. Error(position, "duplicate active flag")
  7641. ELSE
  7642. recordBody.SetActive(TRUE); SetProtectedRecord;
  7643. END;
  7644. ELSIF name=Global.NameSafe THEN
  7645. IF recordBody = NIL THEN
  7646. Error(position, "safe not in record body");
  7647. ELSIF recordBody.isSafe THEN
  7648. Error(position, "duplicate safe flag")
  7649. ELSE
  7650. recordBody.SetSafe(TRUE);
  7651. SetProtectedRecord;
  7652. END;
  7653. ELSIF name=Global.NameRealtime THEN
  7654. IF recordBody = NIL THEN
  7655. Error(position, "realtime not in record body");
  7656. ELSIF recordBody.isRealtime THEN
  7657. Error(position, "duplicate realtime flag")
  7658. ELSE
  7659. recordBody.SetRealtime(TRUE);
  7660. block.SetRealtime(TRUE);
  7661. END;
  7662. ELSIF name=Global.NameUnchecked THEN
  7663. IF block.isUnchecked THEN
  7664. Error(position, "duplicate unchecked flag")
  7665. ELSE
  7666. block.SetUnchecked(TRUE);
  7667. END;
  7668. ELSIF (name=Global.NameUncooperative) THEN
  7669. IF block.isUncooperative THEN
  7670. Error(position, "duplicate uncooperative flag")
  7671. ELSE
  7672. block.SetUncooperative(TRUE);
  7673. END;
  7674. ELSE
  7675. Error(position, "unknown block modifier");
  7676. END;
  7677. blockModifier := blockModifier.nextModifier;
  7678. END;
  7679. END BlockFlags;
  7680. (** check and resolve statement block
  7681. - check flags (exclusive)
  7682. - check statement sequence
  7683. **)
  7684. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7685. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7686. BEGIN
  7687. BlockFlags(statementBlock);
  7688. IF statementBlock.isExclusive THEN
  7689. (* check that not in exclusive block *)
  7690. IF currentIsExclusive THEN
  7691. Error (statementBlock.position,"forbidden recursive exclusive")
  7692. ELSIF currentIsRealtime THEN
  7693. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7694. END;
  7695. END;
  7696. recentExclusive := currentIsExclusive;
  7697. recentUnreachable := currentIsUnreachable;
  7698. recentRealtime := currentIsRealtime;
  7699. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7700. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7701. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7702. StatementSequence(statementBlock.statements);
  7703. currentIsRealtime := recentRealtime;
  7704. currentIsExclusive := recentExclusive;
  7705. currentIsUnreachable := recentUnreachable;
  7706. END VisitStatementBlock;
  7707. (** check and resolve body
  7708. - check flags (active, priority, safe)
  7709. - check body and finally part
  7710. **)
  7711. PROCEDURE Body(body: SyntaxTree.Body);
  7712. BEGIN
  7713. VisitStatementBlock(body);
  7714. IF body.isActive THEN
  7715. IF ~currentIsBodyProcedure THEN
  7716. Error(body.position,"active flag not in object body");
  7717. ELSIF body.priority # NIL THEN
  7718. body.SetPriority(ConstantInteger(body.priority));
  7719. END;
  7720. ELSIF body.isSafe THEN
  7721. Error(body.position,"safe flag not in active body");
  7722. ELSIF body.priority # NIL THEN
  7723. Error(body.position,"priority flag not in active body");
  7724. END;
  7725. IF body.code # NIL THEN
  7726. CheckSystemImport(body.position);
  7727. END;
  7728. StatementSequence(body.finally)
  7729. END Body;
  7730. (*** scopes ***)
  7731. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7732. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7733. VAR duplicateSymbol: BOOLEAN;
  7734. BEGIN
  7735. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7736. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7737. Error(symbol.position,"globally defined keyword")
  7738. END;
  7739. scope.EnterSymbol(symbol,duplicateSymbol);
  7740. IF ~allowDuplicate & duplicateSymbol THEN
  7741. Error(symbol.position,"Multiply defined identifier.");
  7742. IF VerboseErrorMessage THEN
  7743. Printout.Info("multiply defined identifier",symbol);
  7744. Printout.Info("in scope",scope);
  7745. END;
  7746. END;
  7747. END Register;
  7748. (**
  7749. implementation: check and resolve an implementation part
  7750. **)
  7751. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7752. move implementation checker to a separate object ? *)
  7753. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7754. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7755. BEGIN
  7756. prevIsRealtime := currentIsRealtime;
  7757. prevIsBodyProcedure := currentIsBodyProcedure;
  7758. prevIsCellNet := currentIsCellNet;
  7759. prevScope := currentScope;
  7760. currentScope := scope;
  7761. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7762. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7763. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7764. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7765. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7766. (*
  7767. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7768. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7769. END;
  7770. *)
  7771. END;
  7772. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7773. (* module body, record bodies are wrapped into an artifical procedure *)
  7774. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7775. Body(scope(SyntaxTree.ProcedureScope).body)
  7776. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7777. Body(scope(SyntaxTree.ProcedureScope).body)
  7778. END;
  7779. END;
  7780. currentScope := prevScope;
  7781. currentIsRealtime := prevIsRealtime;
  7782. currentIsBodyProcedure := prevIsBodyProcedure;
  7783. currentIsCellNet := prevIsCellNet;
  7784. END Implementation;
  7785. (** implementation phase:
  7786. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7787. **)
  7788. PROCEDURE Implementations(x: SyntaxTree.Module);
  7789. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7790. BEGIN
  7791. prevPhase := phase;
  7792. phase := InlinePhase;
  7793. scope := x.firstScope;
  7794. WHILE(scope # NIL) DO
  7795. Implementation(scope);
  7796. scope := scope.nextScope;
  7797. END;
  7798. phase := ImplementationPhase;
  7799. scope := x.firstScope;
  7800. WHILE(scope # NIL) DO
  7801. Implementation(scope);
  7802. scope := scope.nextScope;
  7803. END;
  7804. phase := prevPhase;
  7805. END Implementations;
  7806. (** declaration phase:
  7807. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7808. - import lists (for module scopes)
  7809. - parameter list (for procedure scopes)
  7810. - constant declarations
  7811. - type declarations
  7812. - variable declarations
  7813. - procedure declarations
  7814. preformed in two stages:
  7815. - first all symbols are entered into the symbol table (with uniqueness check),
  7816. - then all symbols are resolved
  7817. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7818. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7819. phases :
  7820. 0 = before procedures
  7821. 1 = procedures and later
  7822. **)
  7823. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7824. VAR
  7825. constant: SyntaxTree.Constant;
  7826. typeDeclaration: SyntaxTree.TypeDeclaration;
  7827. declaredType: SyntaxTree.Type;
  7828. variable: SyntaxTree.Variable;
  7829. procedure: SyntaxTree.Procedure;
  7830. procedureType : SyntaxTree.ProcedureType;
  7831. prevScope: SyntaxTree.Scope;
  7832. parameter: SyntaxTree.Parameter;
  7833. import: SyntaxTree.Import;
  7834. symbol: SyntaxTree.Symbol;
  7835. prevPhase: LONGINT;
  7836. prevError : BOOLEAN;
  7837. i: LONGINT;
  7838. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7839. VAR baseType: SyntaxTree.Type; property: SyntaxTree.Property;
  7840. BEGIN
  7841. IF type.baseType # NIL THEN
  7842. baseType := type.baseType.resolved;
  7843. IF baseType IS SyntaxTree.PointerType THEN
  7844. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7845. END;
  7846. (*
  7847. IF baseType IS SyntaxTree.CellType THEN
  7848. DeclareCell(baseType(SyntaxTree.CellType));
  7849. END;
  7850. *)
  7851. END;
  7852. parameter := type.firstParameter;
  7853. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7854. (*
  7855. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7856. variable.SetType(parameter.type);
  7857. variable.SetAccess(SyntaxTree.Hidden);
  7858. variable.SetModifiers(parameter.modifiers);
  7859. currentScope.PushVariable(variable);
  7860. *)
  7861. Register(parameter,scope, FALSE);
  7862. parameter := parameter.nextParameter;
  7863. END;
  7864. property := type.firstProperty;
  7865. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7866. (*
  7867. variable := currentScope.FindVariable(property.name);
  7868. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7869. prop := variable(SyntaxTree.Property);
  7870. ELSE (* add, duplicate symbols detection later *)
  7871. prop := SyntaxTree.NewProperty(property.position, property.name);
  7872. currentScope.PushVariable(prop);
  7873. END;
  7874. prop.SetType(property.type);
  7875. prop.SetValue(property.value);
  7876. prop.SetAccess(SyntaxTree.Hidden);
  7877. *)
  7878. Register(property, scope, FALSE);
  7879. property := property.nextProperty;
  7880. END;
  7881. END DeclareCell;
  7882. BEGIN
  7883. prevError := error;
  7884. prevPhase := phase;
  7885. phase := DeclarationPhase;
  7886. prevScope := currentScope;
  7887. currentScope := scope;
  7888. error := FALSE;
  7889. IF 0 IN phases THEN
  7890. (* first enter all symbols in scope *)
  7891. IF scope IS SyntaxTree.ModuleScope THEN
  7892. (* treat imports first for a module scope, , set default context if necessary *)
  7893. import := scope(SyntaxTree.ModuleScope).firstImport;
  7894. WHILE(import # NIL) DO
  7895. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7896. Register(import, currentScope, FALSE);
  7897. import := import.nextImport;
  7898. END;
  7899. import := scope(SyntaxTree.ModuleScope).firstImport;
  7900. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7901. ResolveSymbol(import);
  7902. import := import.nextImport;
  7903. END;
  7904. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7905. (* enter parameters for a procedure scope *)
  7906. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7907. parameter := procedureType.firstParameter;
  7908. WHILE(parameter # NIL) DO
  7909. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7910. END;
  7911. parameter := procedureType.returnParameter;
  7912. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7913. parameter := procedureType.selfParameter;
  7914. IF parameter # NIL THEN
  7915. Register(parameter, currentScope, FALSE);
  7916. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7917. END;
  7918. ELSIF scope IS SyntaxTree.CellScope THEN
  7919. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7920. IF~skipImplementation THEN
  7921. import := scope(SyntaxTree.CellScope).firstImport;
  7922. WHILE(import # NIL) DO
  7923. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7924. Register(import, currentScope, FALSE);
  7925. import := import.nextImport;
  7926. END;
  7927. import := scope(SyntaxTree.CellScope).firstImport;
  7928. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7929. ResolveSymbol(import);
  7930. import := import.nextImport;
  7931. END;
  7932. END;
  7933. END;
  7934. IF error THEN RETURN END;
  7935. IF skipImplementation THEN
  7936. scope.Clear;
  7937. END;
  7938. (* constants *)
  7939. constant := scope.firstConstant;
  7940. WHILE (constant # NIL) DO
  7941. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7942. END;
  7943. (* type declarations *)
  7944. typeDeclaration := scope.firstTypeDeclaration;
  7945. WHILE (typeDeclaration # NIL) DO
  7946. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7947. END;
  7948. (* variables *)
  7949. variable := scope.firstVariable;
  7950. WHILE (variable # NIL) DO
  7951. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7952. END;
  7953. (* procedures *)
  7954. IF scope.procedures # NIL THEN
  7955. FOR i := 0 TO scope.procedures.Length()-1 DO
  7956. procedure := scope.procedures.GetProcedure(i);
  7957. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7958. IF procedureType.selfParameter = NIL THEN
  7959. scope.AddProcedure(procedure);
  7960. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7961. ELSE
  7962. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7963. IF typeDeclaration = NIL THEN
  7964. Error(procedureType.selfParameter.position, "No such type declaration");
  7965. ELSE
  7966. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7967. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7968. declaredType := typeDeclaration.declaredType;
  7969. IF declaredType IS SyntaxTree.PointerType THEN
  7970. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7971. END;
  7972. IF declaredType IS SyntaxTree.RecordType THEN
  7973. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7974. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7975. ELSE
  7976. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7977. END;
  7978. END;
  7979. END;
  7980. END;
  7981. END;
  7982. END;
  7983. (* now process all symbols without any presumption on the order *)
  7984. symbol := scope.firstSymbol;
  7985. WHILE(symbol # NIL) DO
  7986. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7987. IF (symbol IS SyntaxTree.Procedure) THEN
  7988. IF 1 IN phases THEN
  7989. ResolveSymbol(symbol);
  7990. END;
  7991. ELSE
  7992. IF 0 IN phases THEN
  7993. ResolveSymbol(symbol);
  7994. END;
  7995. END;
  7996. END;
  7997. symbol := symbol.nextSymbol;
  7998. END;
  7999. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  8000. symbol := scope.firstSymbol;
  8001. WHILE symbol # NIL DO
  8002. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  8003. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  8004. pointerFixes.Add(symbol, currentScope);
  8005. END;
  8006. IF ~symbol.type.resolved.isRealtime THEN
  8007. Error(symbol.position,"symbol has no realtime type");
  8008. END;
  8009. END;
  8010. symbol := symbol.nextSymbol
  8011. END;
  8012. END;
  8013. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  8014. Error(Basic.invalidPosition,"problems during offset computation in module");
  8015. END;
  8016. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  8017. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  8018. scope.ownerModule.AddScope(scope);
  8019. END;
  8020. phase := prevPhase;
  8021. currentScope := prevScope;
  8022. error := error OR prevError;
  8023. END Declarations;
  8024. (* nopov *)
  8025. (** check if all operators from one module are compatible to the ones in the other module
  8026. - check if there are not multiple operators with the same signature
  8027. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  8028. - check for all operators whose signatures are compatible, whether the return types are compatible
  8029. note that:
  8030. - the return type is not considered to be part of the signature
  8031. - two signatures are considered compatible, if all of the operands are compatible
  8032. **)
  8033. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  8034. VAR
  8035. thisOperator, thatOperator: SyntaxTree.Operator;
  8036. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  8037. thisParameter, thatParameter: SyntaxTree.Parameter;
  8038. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8039. i: LONGINT;
  8040. BEGIN
  8041. currentScope := thisModuleScope;
  8042. hasError := FALSE;
  8043. (* go through all operators in the other module *)
  8044. thatOperator := thatModuleScope.firstOperator;
  8045. WHILE (thatOperator # NIL) & ~hasError DO
  8046. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8047. (* the other operator is accessible *)
  8048. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8049. (* the other operator is not the conversion operator *)
  8050. (* go through all operators in this module *)
  8051. thisOperator := thisModuleScope.firstOperator;
  8052. WHILE (thisOperator # NIL) & ~hasError DO
  8053. IF thisOperator # thatOperator THEN
  8054. (* the operators are not the same *)
  8055. IF thisOperator.name = thatOperator.name THEN
  8056. (* the operators share the same identifier *)
  8057. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8058. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8059. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8060. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8061. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8062. (* both operators have the same paramter count *)
  8063. thisParameter := thisProcedureType.firstParameter;
  8064. thatParameter := thatProcedureType.firstParameter;
  8065. operandsAreEqual := TRUE;
  8066. operandsAreCompatible := TRUE;
  8067. (* go through all parameters *)
  8068. FOR i := 1 TO thisProcedureType.numberParameters DO
  8069. ASSERT(thatParameter # NIL);
  8070. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8071. operandsAreEqual := FALSE;
  8072. IF TypeDistance(system, thisParameter.type, thatParameter.type, thisParameter.kind = SyntaxTree.VarParameter) =Infinity THEN
  8073. operandsAreCompatible := FALSE
  8074. END
  8075. END;
  8076. thisParameter := thisParameter.nextParameter;
  8077. thatParameter := thatParameter.nextParameter
  8078. END;
  8079. IF operandsAreEqual THEN
  8080. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8081. hasError := TRUE
  8082. ELSIF operandsAreCompatible THEN
  8083. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8084. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8085. hasError := TRUE
  8086. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8087. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8088. hasError := TRUE
  8089. END
  8090. END
  8091. END
  8092. END
  8093. END;
  8094. thisOperator := thisOperator.nextOperator
  8095. END
  8096. END
  8097. END;
  8098. thatOperator := thatOperator.nextOperator
  8099. END
  8100. END CheckInterOperatorConformity;
  8101. (** check module:
  8102. - check module declaration
  8103. - add context, if necessary
  8104. - remove module from import cache, if necessary
  8105. - check declarations
  8106. - resolve all type fixes
  8107. - check implementation (bodies)
  8108. **)
  8109. PROCEDURE Module*(x: SyntaxTree.Module);
  8110. VAR (* nopov *)
  8111. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8112. BEGIN
  8113. prevScope := currentScope;
  8114. prevIsCellNet := currentIsCellNet;
  8115. module := x;
  8116. ASSERT(x # NIL);
  8117. global := system.globalScope[x.case];
  8118. x.moduleScope.SetGlobalScope(global);
  8119. currentScope := global;
  8120. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8121. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8122. RemoveModuleFromCache(importCache,x);
  8123. Declarations(x.moduleScope, FALSE, {0,1});
  8124. FixTypes();
  8125. IF module.isCellNet THEN
  8126. currentIsCellNet := TRUE;
  8127. modifier := x.modifiers;
  8128. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8129. CheckModifiers(modifier, FALSE);
  8130. END;
  8131. (* nopov *)
  8132. IF ~error THEN
  8133. (* check if operators conform to each other within this module *)
  8134. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8135. (* go through all imports *)
  8136. import := x.moduleScope.firstImport;
  8137. WHILE import # NIL DO
  8138. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8139. (* check if all operators in this module conform to the ones of the imported module *)
  8140. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8141. END;
  8142. import := import.nextImport
  8143. END;
  8144. END;
  8145. Implementations(x);
  8146. module := NIL;
  8147. currentIsCellNet := prevIsCellNet;
  8148. currentScope := prevScope;
  8149. END Module;
  8150. END Checker;
  8151. Warnings*=OBJECT (SyntaxTree.Visitor)
  8152. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8153. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8154. BEGIN
  8155. SELF.diagnostics := diagnostics
  8156. END InitWarnings;
  8157. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  8158. BEGIN END VisitPortType;
  8159. (** types *)
  8160. PROCEDURE Type(x: SyntaxTree.Type);
  8161. BEGIN x.Accept(SELF)
  8162. END Type;
  8163. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8164. BEGIN END VisitType;
  8165. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8166. BEGIN END VisitBasicType;
  8167. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8168. BEGIN END VisitCharacterType;
  8169. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8170. BEGIN END VisitIntegerType;
  8171. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8172. BEGIN END VisitFloatType;
  8173. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8174. BEGIN END VisitQualifiedType;
  8175. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8176. BEGIN END VisitStringType;
  8177. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8178. BEGIN END VisitEnumerationType;
  8179. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8180. BEGIN END VisitRangeType;
  8181. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8182. BEGIN
  8183. IF ~(SyntaxTree.Warned IN x.state) THEN
  8184. x.SetState(SyntaxTree.Warned);
  8185. Type(x.arrayBase);
  8186. END;
  8187. END VisitArrayType;
  8188. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8189. BEGIN
  8190. IF ~(SyntaxTree.Warned IN x.state) THEN
  8191. x.SetState(SyntaxTree.Warned);
  8192. Type(x.arrayBase);
  8193. END;
  8194. END VisitMathArrayType;
  8195. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8196. BEGIN
  8197. IF ~(SyntaxTree.Warned IN x.state) THEN
  8198. x.SetState(SyntaxTree.Warned);
  8199. Type(x.pointerBase);
  8200. END;
  8201. END VisitPointerType;
  8202. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8203. BEGIN Scope(x.recordScope) END VisitRecordType;
  8204. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8205. BEGIN Scope(x.cellScope) END VisitCellType;
  8206. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8207. BEGIN END VisitProcedureType;
  8208. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8209. VAR msg: ARRAY 256 OF CHAR;
  8210. BEGIN
  8211. Global.GetSymbolName(x,msg);
  8212. Strings.Append(msg," ");
  8213. Strings.Append(msg,text);
  8214. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8215. END Warning;
  8216. (** symbols *)
  8217. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8218. BEGIN
  8219. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8220. IF ~(x IS SyntaxTree.Parameter) THEN
  8221. Warning(x,"never used");
  8222. END;
  8223. END;
  8224. x.Accept(SELF);
  8225. END Symbol;
  8226. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8227. BEGIN END VisitSymbol;
  8228. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8229. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8230. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8231. BEGIN END VisitConstant;
  8232. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8233. BEGIN END VisitVariable;
  8234. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8235. BEGIN END VisitProperty;
  8236. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8237. BEGIN END VisitParameter;
  8238. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8239. BEGIN
  8240. Scope(x.procedureScope)
  8241. END VisitProcedure;
  8242. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8243. BEGIN END VisitOperator;
  8244. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8245. BEGIN END VisitImport;
  8246. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8247. VAR
  8248. symbol: SyntaxTree.Symbol;
  8249. BEGIN
  8250. symbol := scope.firstSymbol;
  8251. WHILE(symbol # NIL) DO
  8252. Symbol(symbol);
  8253. symbol := symbol.nextSymbol;
  8254. END;
  8255. END Scope;
  8256. PROCEDURE Module*(x: SyntaxTree.Module);
  8257. BEGIN
  8258. SELF.module := x;
  8259. Scope(x.moduleScope);
  8260. END Module;
  8261. END Warnings;
  8262. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8263. BEGIN
  8264. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8265. END IsOberonInline;
  8266. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8267. BEGIN
  8268. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8269. END Resolved;
  8270. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  8271. VAR i: LONGINT;
  8272. BEGIN
  8273. i := 1;
  8274. WHILE i < x DO
  8275. i := i *2
  8276. END;
  8277. RETURN i=x
  8278. END PowerOf2;
  8279. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8280. BEGIN
  8281. RETURN
  8282. (scope # NIL) &
  8283. (scope IS SyntaxTree.ModuleScope)
  8284. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8285. OR
  8286. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8287. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8288. END IsCellNetScope;
  8289. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8290. BEGIN
  8291. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8292. END IsCellScope;
  8293. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8294. BEGIN
  8295. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8296. RETURN (scope # NIL) & IsCellNetScope(scope)
  8297. END InCellNetScope;
  8298. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8299. BEGIN
  8300. ASSERT(size MOD system.dataUnit = 0);
  8301. RETURN size DIV system.dataUnit
  8302. END ToMemoryUnits;
  8303. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8304. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8305. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8306. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8307. BEGIN
  8308. IF t = NIL THEN
  8309. RETURN TRUE
  8310. ELSE
  8311. t := t.resolved;
  8312. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8313. END;
  8314. END TypeAllowed;
  8315. BEGIN
  8316. type := type.resolved;
  8317. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8318. RETURN FALSE
  8319. ELSE
  8320. procedureType := type(SyntaxTree.ProcedureType);
  8321. numberParameters := procedureType.numberParameters;
  8322. RETURN
  8323. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8324. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8325. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8326. END;
  8327. END GetProcedureAllowed;
  8328. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8329. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8330. VAR import: SyntaxTree.Import;
  8331. BEGIN
  8332. import := importCache.ImportByModuleName(x.name,x.context);
  8333. IF import # NIL THEN
  8334. importCache.RemoveImporters(x.name,x.context);
  8335. END;
  8336. END RemoveModuleFromCache;
  8337. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8338. (* to <- this assignment compatibility *)
  8339. VAR result: BOOLEAN;
  8340. BEGIN
  8341. IF this= NIL THEN result := (to=NIL)
  8342. ELSIF to=NIL THEN result := FALSE
  8343. ELSE
  8344. (*! will be replaced by this:
  8345. ELSE result := this.CompatibleTo(to.resolved);
  8346. *)
  8347. this := this.resolved; to := to.resolved;
  8348. IF to=SyntaxTree.invalidType THEN result := FALSE
  8349. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8350. ELSIF (to = this) OR (to.SameType(this)) THEN
  8351. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8352. ELSIF to IS SyntaxTree.BasicType THEN
  8353. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8354. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8355. result := this.CompatibleTo(to.resolved)
  8356. ELSE
  8357. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8358. END
  8359. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8360. result := to.sizeInBits >= this.sizeInBits;
  8361. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8362. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8363. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8364. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8365. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8366. result := TRUE;
  8367. ELSIF to IS SyntaxTree.AnyType THEN
  8368. 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);
  8369. ELSIF to IS SyntaxTree.ObjectType THEN
  8370. 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 *) ;
  8371. ELSIF to IS SyntaxTree.ByteType THEN
  8372. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8373. ELSIF to IS SyntaxTree.CharacterType THEN
  8374. result := IsCharacterType(this)
  8375. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8376. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8377. 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)
  8378. OR IsTensor(this)
  8379. ) THEN
  8380. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8381. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8382. result := TRUE;
  8383. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8384. result := TRUE;
  8385. ELSE
  8386. result := FALSE
  8387. END;
  8388. ELSIF IsUnsafePointer(to) & IsUnsafePointer(this) THEN
  8389. result := TRUE;
  8390. ELSIF to IS SyntaxTree.PointerType THEN
  8391. result := (this IS SyntaxTree.NilType) OR
  8392. IsUnsafePointer(to) & ( (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR IsPointerType(this) OR IsTensor(this)) OR
  8393. (IsPointerType(this) & IsTypeExtension(to,this) OR
  8394. ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this))) & (~to.isRealtime OR this.isRealtime);
  8395. ELSIF to IS SyntaxTree.ProcedureType THEN
  8396. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  8397. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8398. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8399. ELSIF to IS SyntaxTree.RecordType THEN
  8400. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8401. ELSIF to IS SyntaxTree.ArrayType THEN
  8402. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8403. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8404. ELSIF StaticArrayCompatible(to, this) THEN
  8405. result := TRUE
  8406. ELSE
  8407. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8408. END;
  8409. ELSIF to IS SyntaxTree.MathArrayType THEN
  8410. IF this IS SyntaxTree.MathArrayType THEN
  8411. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8412. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8413. result := TRUE;
  8414. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8415. result := TRUE;
  8416. ELSE
  8417. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8418. END;
  8419. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8420. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8421. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8422. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8423. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8424. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8425. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8426. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8427. ELSE
  8428. result := FALSE
  8429. END;
  8430. (* an array-structured object type is compatible to the type of its array structure *)
  8431. ELSIF IsArrayStructuredObjectType(this) THEN
  8432. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8433. ELSE
  8434. result := FALSE;
  8435. END;
  8436. ELSIF to IS SyntaxTree.StringType THEN
  8437. result := FALSE;
  8438. ELSIF to IS SyntaxTree.EnumerationType THEN
  8439. result := IsEnumerationExtension(this,to);
  8440. ELSIF to IS SyntaxTree.PortType THEN
  8441. result := SameType(to, this)
  8442. ELSE
  8443. Printout.Info("CompatibleTo",to);
  8444. HALT(100); (* implement missing type check *)
  8445. END;
  8446. END;
  8447. RETURN result
  8448. END CompatibleTo;
  8449. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8450. VAR actualBase, formalBase: SyntaxTree.Type;
  8451. BEGIN
  8452. IF SameType(formal,actual) THEN
  8453. RETURN TRUE
  8454. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8455. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8456. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8457. RETURN
  8458. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8459. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8460. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8461. & StaticArrayCompatible(formalBase,actualBase)
  8462. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8463. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8464. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8465. RETURN
  8466. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8467. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8468. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8469. & StaticArrayCompatible(formalBase,actualBase)
  8470. ELSE RETURN FALSE
  8471. END;
  8472. END StaticArrayCompatible;
  8473. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8474. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8475. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8476. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8477. BEGIN
  8478. result := SameType(formal,actual);
  8479. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8480. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8481. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8482. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8483. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8484. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8485. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8486. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8487. & TC(formalBase, actualBase);
  8488. END;
  8489. RETURN result
  8490. END TC;
  8491. BEGIN
  8492. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8493. ELSE
  8494. arrayBase := formalType.arrayBase.resolved;
  8495. IF (actualType IS SyntaxTree.StringType) THEN
  8496. result := arrayBase IS SyntaxTree.CharacterType
  8497. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8498. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8499. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8500. result := TC(formalType, actualType);
  8501. ELSE
  8502. result := (arrayBase IS SyntaxTree.ByteType)
  8503. END;
  8504. END;
  8505. RETURN result
  8506. END OpenArrayCompatible;
  8507. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8508. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8509. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8510. BEGIN
  8511. IF actualType IS SyntaxTree.MathArrayType THEN
  8512. actualArray := actualType(SyntaxTree.MathArrayType);
  8513. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8514. (*
  8515. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8516. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8517. *)
  8518. actualBase := ArrayBase(actualType,Infinity);
  8519. formalBase := ArrayBase(formalType,Infinity);
  8520. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8521. ELSE
  8522. (*
  8523. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8524. *)
  8525. formalBase := Resolved(formalType.arrayBase);
  8526. actualBase := Resolved(actualArray.arrayBase);
  8527. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8528. (*
  8529. ARRAY [k] -> ARRAY [n]
  8530. *)
  8531. result := (formalType.staticLength = actualArray.staticLength)
  8532. ELSE
  8533. result := TRUE
  8534. END;
  8535. IF ~result THEN
  8536. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8537. ELSIF actualBase = NIL THEN result := FALSE
  8538. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8539. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8540. ELSE
  8541. result := SameType(formalBase,actualBase)
  8542. END;
  8543. END;
  8544. ELSE
  8545. result := FALSE
  8546. END;
  8547. RETURN result
  8548. END MathArrayCompatible;
  8549. (**
  8550. Math Array Type distance for assignments / parameter passings of the form
  8551. from -> to
  8552. variants:
  8553. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8554. allowed:
  8555. static -> static (& size match)
  8556. static -> open
  8557. static -> tensor
  8558. open -> open
  8559. open -> tensor
  8560. open -> static
  8561. tensor -> tensor
  8562. tensor -> open
  8563. tensor -> static
  8564. **)
  8565. (*! think about the metric here: is form matching more important than element type matching? *)
  8566. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8567. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8568. BEGIN
  8569. fromBase := Resolved(from.arrayBase);
  8570. toBase := Resolved(to.arrayBase);
  8571. i := Infinity;
  8572. IF (from = to) OR (from.SameType(to)) THEN
  8573. i := 0;
  8574. ELSIF (from.form = to.form) THEN
  8575. (* static -> static, open -> open, tensor -> tensor *)
  8576. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8577. IF fromBase = toBase THEN i := 0
  8578. ELSIF toBase = NIL THEN i := 1
  8579. ELSIF fromBase = NIL THEN i := Infinity;
  8580. ELSIF toBase.SameType(fromBase) THEN i := 0
  8581. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8582. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8583. ELSE
  8584. i := TypeDistance(system,fromBase, toBase, varpar);
  8585. IF i < Infinity THEN i := i * 5 END;
  8586. END;
  8587. END;
  8588. ELSIF (to.form = SyntaxTree.Static) THEN
  8589. (* forbidden *)
  8590. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8591. (* static -> tensor, open -> tensor, tensor -> open *)
  8592. IF (toBase=fromBase) THEN i := 0;
  8593. ELSIF toBase = NIL THEN i := 1;
  8594. ELSIF toBase.SameType(fromBase) THEN i := 0
  8595. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8596. toBase := ArrayBase(toBase,Infinity);
  8597. IF (fromBase=toBase) THEN i := 0
  8598. ELSIF (toBase = NIL) THEN i:= 1
  8599. ELSIF (fromBase = NIL) THEN i := Infinity;
  8600. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8601. IF i < Infinity THEN i := i * 5 END;
  8602. END;
  8603. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8604. fromBase := ArrayBase(fromBase,Infinity);
  8605. IF (fromBase=toBase) THEN i := 0
  8606. ELSIF (toBase = NIL) THEN i := 1
  8607. ELSIF (fromBase = NIL) THEN i := Infinity;
  8608. ELSIF toBase.SameType(fromBase) THEN i := 0
  8609. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8610. IF i < Infinity THEN i := i * 5 END;
  8611. END;
  8612. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8613. IF i < Infinity THEN i := i * 5 END;
  8614. END;
  8615. IF i # Infinity THEN INC(i,2) END;
  8616. ELSIF (from.form = SyntaxTree.Static) THEN
  8617. (* static -> open *)
  8618. IF (toBase=fromBase) THEN i := 0;
  8619. ELSIF toBase = NIL THEN i := 1
  8620. ELSIF fromBase = NIL THEN i := Infinity
  8621. ELSIF toBase.SameType(fromBase) THEN i := 0
  8622. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8623. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8624. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8625. IF i < Infinity THEN i := i * 5 END;
  8626. END;
  8627. IF i # Infinity THEN INC(i,1) END;
  8628. ELSE HALT(100); (* unknown case *)
  8629. END;
  8630. RETURN i;
  8631. END MathArrayTypeDistance;
  8632. (** compute and return the distance of two array types
  8633. - return the distance of the base types
  8634. **)
  8635. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8636. VAR i: LONGINT;
  8637. BEGIN
  8638. i := Infinity;
  8639. IF from = to THEN
  8640. i := 0
  8641. ELSE
  8642. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8643. (*
  8644. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8645. i := TypeDistance(from.base, to.base);
  8646. IF i >= 0 THEN INC(i) END
  8647. ELSIF (from.mode = open) & (to.mode = open) THEN
  8648. i := TypeDistance(from.base, to.base);
  8649. *)
  8650. END;
  8651. RETURN i
  8652. END ArrayTypeDistance;
  8653. (** compute the signature distance of a procedure and an actual parameter list
  8654. - if any of the parameters are not compatible, the result is infinite
  8655. - add up and return the distance over all parameters
  8656. **)
  8657. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8658. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8659. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8660. BEGIN
  8661. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8662. result := Infinity
  8663. ELSE
  8664. formalParameter := procedureType.firstParameter;
  8665. i := 0;
  8666. result := 0;
  8667. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8668. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8669. actualParameter := actualParameters.GetExpression(i);
  8670. ASSERT(formalParameter.type # NIL);
  8671. IF (actualParameter.type = NIL) THEN distance := Infinity
  8672. ELSE
  8673. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8674. END;
  8675. IF distance = Infinity THEN
  8676. result := Infinity;
  8677. ELSE
  8678. to := formalParameter.type.resolved;
  8679. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8680. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8681. (* already handled varpar *)
  8682. (*
  8683. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8684. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8685. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8686. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8687. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8688. END;
  8689. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8690. result := Infinity
  8691. END;
  8692. *)
  8693. INC(result, distance);
  8694. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8695. INC(result, distance);
  8696. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8697. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8698. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8699. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8700. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8701. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8702. END;
  8703. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8704. result := Infinity
  8705. END;
  8706. ELSE
  8707. result := Infinity
  8708. END;
  8709. ELSE
  8710. INC(result,distance);
  8711. END;
  8712. END;
  8713. (*
  8714. Printout.Info("actual=", actualParameter);
  8715. Printout.Info("formal=", formalParameter);
  8716. TRACE(result);
  8717. *)
  8718. formalParameter := formalParameter.nextParameter; INC(i);
  8719. END;
  8720. END;
  8721. ASSERT(result >= 0);
  8722. RETURN result
  8723. END Distance;
  8724. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8725. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8726. BEGIN
  8727. IF right.numberParameters # (procedureType.numberParameters) THEN
  8728. result := Infinity
  8729. ELSE
  8730. formalParameter := procedureType.firstParameter;
  8731. rightParameter := right.firstParameter;
  8732. i := 0;
  8733. result := 0;
  8734. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8735. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8736. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8737. IF distance = Infinity THEN
  8738. result := Infinity;
  8739. ELSE
  8740. INC(result,distance);
  8741. END;
  8742. formalParameter := formalParameter.nextParameter;
  8743. rightParameter := rightParameter.nextParameter;
  8744. END;
  8745. END;
  8746. ASSERT(result >= 0);
  8747. RETURN result
  8748. END ProcedureTypeDistance;
  8749. (** compute and return the distance between two types, used for computation of signature distance
  8750. from -> to
  8751. **)
  8752. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8753. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8754. BEGIN
  8755. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8756. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8757. END;
  8758. i := Infinity;
  8759. IF from = to THEN
  8760. i := 0
  8761. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8762. ELSIF to.SameType(from) THEN
  8763. i := 0;
  8764. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8765. i := Infinity;
  8766. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8767. i := 10;
  8768. ELSIF (from IS SyntaxTree.StringType) THEN
  8769. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8770. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8771. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8772. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8773. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8774. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8775. i := 1
  8776. ELSIF (from IS SyntaxTree.NilType) THEN
  8777. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8778. (*
  8779. ELSIF (from = NoType) THEN
  8780. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8781. *)
  8782. ELSIF (from IS SyntaxTree.BasicType) THEN
  8783. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8784. IF varpar & (i # 0) THEN i := Infinity END;
  8785. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8786. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8787. ELSIF (from IS SyntaxTree.RecordType) THEN
  8788. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8789. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8790. IF to IS SyntaxTree.MathArrayType THEN
  8791. (*
  8792. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8793. i := Infinity;
  8794. ELSE
  8795. *)
  8796. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8797. (*
  8798. END;
  8799. *)
  8800. END
  8801. ELSIF (from IS SyntaxTree.PointerType) THEN
  8802. ptr := from(SyntaxTree.PointerType);
  8803. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8804. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8805. (* ELSE i := TypeDistance(ptr.base, to); *)
  8806. END
  8807. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8808. IF (to IS SyntaxTree.ProcedureType) THEN
  8809. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8810. END;
  8811. ELSIF (from IS SyntaxTree.PortType) THEN
  8812. IF (to IS SyntaxTree.PortType) THEN
  8813. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8814. i := 0;
  8815. END;
  8816. END;
  8817. (*no procedure test, procedure must be the same*)
  8818. END;
  8819. RETURN i
  8820. END TypeDistance;
  8821. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8822. BEGIN
  8823. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8824. END IsIntegerType;
  8825. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8826. BEGIN
  8827. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8828. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8829. OR IsPointerType(type)
  8830. )
  8831. END IsAddressType;
  8832. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8833. BEGIN
  8834. RETURN (type # NIL) & ((type.resolved IS SyntaxTree.IntegerType) & (type.resolved(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type.resolved IS SyntaxTree.SizeType))
  8835. END IsSizeType;
  8836. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8837. BEGIN
  8838. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8839. END IsSignedIntegerType;
  8840. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8841. BEGIN
  8842. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8843. END IsUnsignedIntegerType;
  8844. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8845. VAR result: BOOLEAN;
  8846. BEGIN
  8847. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8848. value := x.resolved(SyntaxTree.IntegerValue).value;
  8849. result := TRUE
  8850. ELSE
  8851. result := FALSE
  8852. END;
  8853. RETURN result
  8854. END IsIntegerValue;
  8855. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8856. VAR result: BOOLEAN;
  8857. BEGIN
  8858. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8859. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8860. result := TRUE
  8861. ELSE
  8862. result := FALSE
  8863. END;
  8864. RETURN result
  8865. END IsEnumerationValue;
  8866. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8867. VAR result: BOOLEAN;
  8868. BEGIN
  8869. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8870. value := x.resolved(SyntaxTree.RealValue).value;
  8871. result := TRUE
  8872. ELSE
  8873. result := FALSE
  8874. END;
  8875. RETURN result
  8876. END IsRealValue;
  8877. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8878. VAR result: BOOLEAN;
  8879. BEGIN
  8880. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8881. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8882. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8883. result := TRUE
  8884. ELSE
  8885. result := FALSE
  8886. END;
  8887. RETURN result
  8888. END IsComplexValue;
  8889. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8890. VAR result: BOOLEAN;
  8891. BEGIN
  8892. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8893. value := x.resolved(SyntaxTree.CharacterValue).value;
  8894. result := TRUE
  8895. ELSE
  8896. result := FALSE
  8897. END;
  8898. RETURN result
  8899. END IsCharacterValue;
  8900. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8901. VAR result: BOOLEAN;
  8902. BEGIN
  8903. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8904. value := x.resolved(SyntaxTree.BooleanValue).value;
  8905. result := TRUE
  8906. ELSE
  8907. result := FALSE
  8908. END;
  8909. RETURN result
  8910. END IsBooleanValue;
  8911. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8912. VAR result: BOOLEAN;
  8913. BEGIN
  8914. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8915. value := x.resolved(SyntaxTree.SetValue).value;
  8916. result := TRUE
  8917. ELSE
  8918. result := FALSE
  8919. END;
  8920. RETURN result
  8921. END IsSetValue;
  8922. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8923. VAR result: BOOLEAN;
  8924. BEGIN
  8925. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8926. value := x.resolved(SyntaxTree.StringValue).value;
  8927. result := TRUE
  8928. ELSE
  8929. result := FALSE
  8930. END;
  8931. RETURN result
  8932. END IsStringValue;
  8933. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8934. BEGIN
  8935. x := x.resolved;
  8936. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8937. END Indexable;
  8938. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8939. BEGIN
  8940. RETURN t1.SameType(t2.resolved);
  8941. END SameType;
  8942. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8943. BEGIN
  8944. IF t IS SyntaxTree.MathArrayType THEN
  8945. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8946. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8947. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8948. DEC(max);
  8949. END;
  8950. ELSIF t IS SyntaxTree.ArrayType THEN
  8951. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8952. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8953. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8954. END;
  8955. END;
  8956. RETURN t;
  8957. END ArrayBase;
  8958. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8959. BEGIN
  8960. type := type.resolved;
  8961. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8962. base := type(SyntaxTree.ArrayType).arrayBase;
  8963. RETURN TRUE;
  8964. END;
  8965. RETURN FALSE;
  8966. END IsOpenArray;
  8967. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8968. BEGIN
  8969. type := type.resolved;
  8970. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8971. base := type(SyntaxTree.ArrayType).arrayBase;
  8972. dim := type(SyntaxTree.ArrayType).staticLength;
  8973. RETURN TRUE
  8974. ELSE
  8975. RETURN FALSE
  8976. END;
  8977. END IsStaticArray;
  8978. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8979. BEGIN
  8980. type := type.resolved;
  8981. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8982. base := type(SyntaxTree.ArrayType).arrayBase;
  8983. RETURN TRUE
  8984. ELSE
  8985. RETURN FALSE
  8986. END;
  8987. END IsDynamicArray;
  8988. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8989. VAR i: LONGINT;
  8990. BEGIN
  8991. i := 0;
  8992. t := t.resolved;
  8993. IF t IS SyntaxTree.MathArrayType THEN
  8994. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8995. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8996. END;
  8997. ELSIF t IS SyntaxTree.ArrayType THEN
  8998. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8999. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  9000. END;
  9001. END;
  9002. RETURN i
  9003. END Dimension;
  9004. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  9005. BEGIN
  9006. RETURN expression.assignable;
  9007. END IsVariable;
  9008. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9009. BEGIN
  9010. IF (symbol IS SyntaxTree.Parameter) THEN
  9011. WITH symbol: SyntaxTree.Parameter DO
  9012. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  9013. END;
  9014. ELSE
  9015. RETURN FALSE
  9016. END;
  9017. END IsVariableParameter;
  9018. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  9019. VAR result: BOOLEAN;
  9020. BEGIN
  9021. IF type = NIL THEN result := FALSE
  9022. ELSE
  9023. type := type.resolved;
  9024. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  9025. END;
  9026. RETURN result
  9027. END IsPointerType;
  9028. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  9029. VAR result: BOOLEAN;
  9030. BEGIN
  9031. IF type = NIL THEN result := FALSE
  9032. ELSE
  9033. type := type.resolved;
  9034. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe OR
  9035. (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9036. & type(SyntaxTree.MathArrayType).isUnsafe;
  9037. END;
  9038. RETURN result
  9039. END IsUnsafePointer;
  9040. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  9041. BEGIN
  9042. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  9043. END IsDisposable;
  9044. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  9045. VAR result: BOOLEAN;
  9046. BEGIN
  9047. IF type = NIL THEN result := FALSE
  9048. ELSE
  9049. type := type.resolved;
  9050. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  9051. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9052. result := result OR (type IS SyntaxTree.ObjectType);
  9053. END;
  9054. RETURN result
  9055. END IsPointerToRecord;
  9056. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  9057. VAR result: BOOLEAN;
  9058. BEGIN
  9059. IF type = NIL THEN result := FALSE
  9060. ELSE
  9061. type := type.resolved;
  9062. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  9063. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  9064. ;
  9065. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9066. result := result OR (type IS SyntaxTree.ObjectType);
  9067. END;
  9068. RETURN result
  9069. END IsPointerToObject;
  9070. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9071. BEGIN
  9072. IF type # NIL THEN
  9073. RETURN type.resolved.hasPointers
  9074. ELSE
  9075. RETURN FALSE
  9076. END;
  9077. END ContainsPointer;
  9078. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9079. BEGIN
  9080. IF type = NIL THEN RETURN FALSE END;
  9081. type := type.resolved;
  9082. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9083. END IsStringType;
  9084. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9085. BEGIN
  9086. IF type = NIL THEN RETURN FALSE END;
  9087. type := type.resolved;
  9088. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9089. END IsCharacterType;
  9090. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9091. BEGIN
  9092. IF type = NIL THEN RETURN FALSE END;
  9093. type := type.resolved;
  9094. RETURN (type IS SyntaxTree.EnumerationType)
  9095. END IsEnumerationType;
  9096. (** cf. section "Type extension (base type)" in the language report **)
  9097. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9098. VAR result: BOOLEAN;
  9099. BEGIN
  9100. ASSERT(base # NIL); ASSERT(extension # NIL);
  9101. base := base.resolved; extension := extension.resolved;
  9102. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9103. result := TRUE;
  9104. ELSE
  9105. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9106. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9107. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9108. END;
  9109. WHILE (extension # NIL) & (extension # base) DO
  9110. IF extension IS SyntaxTree.RecordType THEN
  9111. extension := extension(SyntaxTree.RecordType).baseType;
  9112. IF (extension # NIL) THEN extension := extension.resolved END;
  9113. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9114. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9115. END;
  9116. ELSE extension := NIL;
  9117. END;
  9118. END;
  9119. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9120. END;
  9121. RETURN result
  9122. END IsTypeExtension;
  9123. (** check if base is the base enumeration type of extension **)
  9124. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9125. BEGIN
  9126. base := base.resolved; extension := extension.resolved;
  9127. WHILE (extension # NIL) & (extension # base) DO
  9128. IF extension IS SyntaxTree.EnumerationType THEN
  9129. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9130. IF extension # NIL THEN extension := extension.resolved END;
  9131. ELSE
  9132. extension := NIL
  9133. END;
  9134. END;
  9135. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9136. END IsEnumerationExtension;
  9137. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9138. BEGIN
  9139. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9140. RETURN TRUE
  9141. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9142. RETURN TRUE
  9143. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9144. RETURN TRUE
  9145. ELSE
  9146. RETURN FALSE
  9147. END
  9148. END IsCallable;
  9149. (** compute and return the distance of two record types
  9150. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9151. **)
  9152. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9153. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9154. BEGIN
  9155. i := 0;
  9156. WHILE (from # NIL) & (from # to) DO
  9157. baseType := from.baseType;
  9158. IF (baseType # NIL) THEN
  9159. baseType := baseType.resolved;
  9160. IF baseType IS SyntaxTree.PointerType THEN
  9161. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9162. END;
  9163. IF baseType IS SyntaxTree.RecordType THEN
  9164. from := baseType(SyntaxTree.RecordType);
  9165. ELSE
  9166. from := NIL;
  9167. END;
  9168. ELSE
  9169. from := NIL
  9170. END;
  9171. INC(i)
  9172. END;
  9173. IF from = NIL THEN i := Infinity END;
  9174. RETURN i
  9175. END RecordTypeDistance;
  9176. (** compute and return the distance of two pointer types **)
  9177. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9178. BEGIN
  9179. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9180. RETURN Infinity;
  9181. ELSE
  9182. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9183. END;
  9184. END PointerTypeDistance;
  9185. (** check if expression contains a symbol designator pointing to a type declaration.
  9186. - if so then enter type declaration into typeDeclaration and return true else return false
  9187. **)
  9188. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9189. VAR result: BOOLEAN;
  9190. BEGIN
  9191. result := FALSE;
  9192. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9193. result := TRUE;
  9194. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9195. END;
  9196. RETURN result
  9197. END IsTypeDesignator;
  9198. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9199. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9200. VAR result: BOOLEAN;
  9201. BEGIN
  9202. type := type.resolved;
  9203. IF type IS SyntaxTree.PointerType THEN
  9204. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9205. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9206. result := TRUE
  9207. ELSE
  9208. result := type IS SyntaxTree.RecordType
  9209. END;
  9210. RETURN result
  9211. END IsExtensibleType;
  9212. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9213. BEGIN
  9214. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9215. (d IS SyntaxTree.SymbolDesignator) &
  9216. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9217. OR
  9218. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9219. END IsUnextensibleRecord;
  9220. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9221. BEGIN
  9222. IF IsUnextensibleRecord(d) THEN
  9223. RETURN FALSE
  9224. ELSE RETURN IsExtensibleType(d.type.resolved)
  9225. END;
  9226. END IsExtensibleDesignator;
  9227. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9228. BEGIN
  9229. type := type.resolved;
  9230. IF (type IS SyntaxTree.PointerType) THEN
  9231. RETURN TRUE
  9232. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9233. RETURN TRUE
  9234. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9235. RETURN TRUE
  9236. ELSIF (type IS SyntaxTree.BasicType) THEN
  9237. RETURN TRUE
  9238. END;
  9239. RETURN FALSE
  9240. END IsBasicType;
  9241. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9242. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9243. BEGIN
  9244. baseType := record.baseType;
  9245. IF (baseType # NIL) THEN
  9246. baseType := baseType.resolved;
  9247. IF (baseType IS SyntaxTree.PointerType) THEN
  9248. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9249. END;
  9250. END;
  9251. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9252. recordType := baseType(SyntaxTree.RecordType);
  9253. ELSE
  9254. recordType := NIL;
  9255. END;
  9256. RETURN recordType
  9257. END RecordBase;
  9258. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9259. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9260. BEGIN
  9261. baseRecord := RecordBase(scope.ownerRecord);
  9262. IF baseRecord = NIL THEN RETURN NIL END;
  9263. scope := baseRecord.recordScope;
  9264. procedureType := procedure.type.resolved;
  9265. IF procedure IS SyntaxTree.Operator THEN
  9266. operator := scope.firstOperator;
  9267. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9268. (*
  9269. Printout.Info("not same ",procedureType);
  9270. Printout.Info("with ",operator.type);
  9271. *)
  9272. operator := operator.nextOperator;
  9273. END;
  9274. super := operator;
  9275. ELSE
  9276. super := scope.firstProcedure;
  9277. WHILE (super # NIL) & (super.name # procedure.name) DO
  9278. super := super.nextProcedure;
  9279. END;
  9280. END;
  9281. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9282. RETURN super
  9283. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9284. RETURN super
  9285. ELSE
  9286. RETURN FindSuperProcedure(scope,procedure);
  9287. END;
  9288. END FindSuperProcedure;
  9289. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9290. VAR procedure: SyntaxTree.Procedure;
  9291. BEGIN
  9292. procedure := record.recordScope.constructor;
  9293. IF procedure = NIL THEN
  9294. record := RecordBase(record);
  9295. IF record # NIL THEN
  9296. procedure := GetConstructor(record)
  9297. END;
  9298. END;
  9299. RETURN procedure;
  9300. END GetConstructor;
  9301. (* enter a case into a list of cases in a sorted way and check for collision *)
  9302. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  9303. VAR prev,this,new: SyntaxTree.CaseConstant;
  9304. BEGIN
  9305. this := root;
  9306. prev := NIL;
  9307. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9308. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9309. RETURN FALSE
  9310. ELSE
  9311. IF (this # NIL) & (this.min = max+1) THEN
  9312. this.min := min
  9313. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9314. prev.max := min
  9315. ELSE
  9316. NEW(new); new.min := min; new.max := max;
  9317. new.next := this;
  9318. IF prev = NIL THEN
  9319. root := new;
  9320. ELSE
  9321. prev.next := new
  9322. END
  9323. END;
  9324. RETURN TRUE
  9325. END;
  9326. END EnterCase;
  9327. (** generate and return a new checker object, errors are entered into diagnostics **)
  9328. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9329. VAR checker: Checker;
  9330. BEGIN
  9331. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9332. RETURN checker
  9333. END NewChecker;
  9334. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9335. VAR warnings: Warnings;
  9336. BEGIN
  9337. NEW(warnings, diagnostics); RETURN warnings;
  9338. END NewWarnings;
  9339. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9340. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9341. END IsRangeType;
  9342. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9343. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9344. END IsMathArrayType;
  9345. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9346. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9347. END IsArrayType;
  9348. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9349. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9350. END IsComplexType;
  9351. (** if a type is an array-structured object type *)
  9352. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9353. VAR recordType: SyntaxTree.RecordType;
  9354. BEGIN
  9355. IF type = NIL THEN
  9356. RETURN FALSE
  9357. ELSE
  9358. type := type.resolved;
  9359. IF type IS SyntaxTree.PointerType THEN
  9360. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9361. IF type IS SyntaxTree.RecordType THEN
  9362. recordType := type(SyntaxTree.RecordType);
  9363. RETURN recordType.isObject & recordType.HasArrayStructure()
  9364. ELSE
  9365. RETURN FALSE
  9366. END
  9367. ELSE
  9368. RETURN FALSE
  9369. END
  9370. END
  9371. END IsArrayStructuredObjectType;
  9372. (** the math array structure of a type
  9373. - for math arrays: the array itself
  9374. - for pointers: the math array structure of the pointer base
  9375. - for array-structured object types: the underlying structure
  9376. - for non-math arrays and all other types: NIL
  9377. **)
  9378. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9379. VAR
  9380. result: SyntaxTree.MathArrayType;
  9381. BEGIN
  9382. IF type = NIL THEN
  9383. result := NIL
  9384. ELSE
  9385. type := type.resolved;
  9386. IF type IS SyntaxTree.PointerType THEN
  9387. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9388. END;
  9389. IF type IS SyntaxTree.MathArrayType THEN
  9390. result := type(SyntaxTree.MathArrayType)
  9391. ELSIF type IS SyntaxTree.RecordType THEN
  9392. result := type(SyntaxTree.RecordType).arrayStructure
  9393. ELSE
  9394. result := NIL
  9395. END
  9396. END;
  9397. RETURN result
  9398. END MathArrayStructureOfType;
  9399. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  9400. VAR
  9401. result: BOOLEAN;
  9402. rangeExpression: SyntaxTree.RangeExpression;
  9403. BEGIN
  9404. IF x IS SyntaxTree.RangeExpression THEN
  9405. rangeExpression := x(SyntaxTree.RangeExpression);
  9406. result := TRUE;
  9407. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9408. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9409. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9410. ELSE
  9411. result := FALSE
  9412. END;
  9413. RETURN result
  9414. END IsStaticRange;
  9415. (** whether a type is a math array of tensor form **)
  9416. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9417. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9418. END IsTensor;
  9419. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9420. BEGIN
  9421. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9422. length := type(SyntaxTree.MathArrayType).staticLength;
  9423. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9424. RETURN TRUE
  9425. ELSE
  9426. RETURN FALSE
  9427. END;
  9428. END IsStaticMathArray;
  9429. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9430. BEGIN
  9431. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9432. END SymbolHasAddress;
  9433. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9434. BEGIN
  9435. RETURN
  9436. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9437. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9438. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9439. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9440. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9441. ;
  9442. END HasAddress;
  9443. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9444. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9445. BEGIN
  9446. IF (e IS SyntaxTree.Designator) THEN
  9447. d := e(SyntaxTree.Designator);
  9448. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9449. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9450. e := d.left;
  9451. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9452. END;
  9453. IF d # NIL THEN
  9454. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9455. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9456. END;
  9457. END;
  9458. RETURN FALSE;
  9459. END IsLocalVariable;
  9460. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9461. BEGIN
  9462. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9463. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9464. ELSE
  9465. RETURN TRUE
  9466. END;
  9467. END IsStaticProcedure;
  9468. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9469. CONST OptimizeMethodTable = FALSE;
  9470. BEGIN
  9471. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9472. END InMethodTable;
  9473. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9474. BEGIN
  9475. IF type = NIL THEN RETURN FALSE
  9476. ELSE
  9477. type := type.resolved;
  9478. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9479. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9480. END
  9481. END ReturnedAsParameter;
  9482. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9483. BEGIN
  9484. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9485. END StructuredReturnType;
  9486. END FoxSemanticChecker.
  9487. System.FreeDownTo FoxSemanticChecker ~