FoxSemanticChecker.Mod 366 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197
  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. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  9. TYPE
  10. Position=SyntaxTree.Position;
  11. FileName=ARRAY 256 OF CHAR;
  12. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  13. p: ANY; scope: SyntaxTree.Scope;
  14. next: LateFix;
  15. END;
  16. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  17. VAR first,last: LateFix;
  18. PROCEDURE & Init;
  19. BEGIN first := NIL; last := NIL;
  20. END Init;
  21. (* get and remove element from list *)
  22. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  23. VAR p: ANY;
  24. BEGIN
  25. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  26. IF first = NIL THEN last := NIL END;
  27. RETURN p;
  28. END Get;
  29. (* add unresolved type to list *)
  30. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  31. VAR next: LateFix;
  32. BEGIN
  33. ASSERT(scope # NIL);
  34. NEW(next); next.p := p; next.scope := scope;
  35. next.next := NIL;
  36. IF first = NIL THEN first := next; last := next;
  37. ELSE last.next := next; last := next
  38. END;
  39. END Add;
  40. END LateFixList;
  41. WithEntry = POINTER TO RECORD
  42. previous: WithEntry;
  43. symbol: SyntaxTree.Symbol;
  44. type: SyntaxTree.Type;
  45. END;
  46. Replacement*= POINTER TO RECORD
  47. name*: Basic.SegmentedName;
  48. expression*: SyntaxTree.Expression;
  49. used*: BOOLEAN;
  50. next*: Replacement;
  51. END;
  52. (** checker object: used to check and resolve a module
  53. - resolves types
  54. - resolves expressions
  55. - resolves designators
  56. - resolves declarations
  57. - resolves statements
  58. - resolves implementations (bodies)
  59. **)
  60. Checker*= OBJECT
  61. VAR
  62. module: SyntaxTree.Module;
  63. diagnostics: Diagnostics.Diagnostics;
  64. cooperative: BOOLEAN;
  65. error-: BOOLEAN;
  66. VerboseErrorMessage: BOOLEAN;
  67. typeFixes, pointerFixes: LateFixList;
  68. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  69. arrayBaseImported: BOOLEAN;
  70. complexNumbersImported: BOOLEAN;
  71. phase: LONGINT;
  72. system-: Global.System;
  73. symbolFileFormat-: Formats.SymbolFileFormat;
  74. backendName-: ARRAY 32 OF CHAR;
  75. inConversion: LONGINT;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  82. currentScope-: SyntaxTree.Scope;
  83. currentIsRealtime: BOOLEAN;
  84. currentIsUnreachable: BOOLEAN;
  85. currentIsCellNet: BOOLEAN;
  86. currentIsBodyProcedure: BOOLEAN;
  87. currentIsExclusive: BOOLEAN;
  88. global: SyntaxTree.ModuleScope;
  89. withEntries: WithEntry;
  90. activeCellsStatement: BOOLEAN;
  91. replacements*: Replacement;
  92. cellsAreObjects: BOOLEAN;
  93. variableAccessed: BOOLEAN;
  94. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  95. BEGIN
  96. SELF.diagnostics := diagnostics;
  97. SELF.cooperative := cooperative;
  98. SELF.system := system;
  99. SELF.symbolFileFormat := symbolFileFormat;
  100. error := FALSE;
  101. NEW(typeFixes);
  102. NEW(pointerFixes);
  103. resolvedStatement := NIL;
  104. currentScope := NIL;
  105. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  106. SELF.importCache := importCache;
  107. arrayBaseImported := FALSE;
  108. complexNumbersImported := FALSE;
  109. SELF.VerboseErrorMessage := verboseErrorMessage;
  110. global := NIL;
  111. phase := UndefinedPhase;
  112. currentIsRealtime := FALSE;
  113. currentIsUnreachable := FALSE;
  114. currentIsCellNet := FALSE;
  115. currentIsBodyProcedure := FALSE;
  116. currentIsExclusive := FALSE;
  117. withEntries := NIL;
  118. SELF.cellsAreObjects := system.cellsAreObjects;
  119. COPY(backend, backendName);
  120. inConversion := 0;
  121. END InitChecker;
  122. (*------------ service -------------*)
  123. (** report error **)
  124. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  125. VAR errModule: SyntaxTree.Module;
  126. BEGIN
  127. ASSERT(currentScope # NIL);
  128. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  129. Basic.ErrorC(diagnostics, errModule.sourceName, position, Basic.InvalidCode, message);
  130. error := TRUE;
  131. END Error;
  132. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  133. VAR errModule: SyntaxTree.Module;
  134. BEGIN
  135. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  136. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  137. END Warning;
  138. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  139. VAR errorMessage: ARRAY 256 OF CHAR;
  140. BEGIN
  141. Basic.Concat(errorMessage,msg," ", msg2);
  142. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  143. error := TRUE;
  144. END ErrorSS;
  145. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  146. VAR msg, msg2: ARRAY 256 OF CHAR;
  147. BEGIN
  148. COPY(msg1, msg);
  149. Strings.Append(msg, " = ");
  150. Basic.GetString(s, msg2);
  151. Strings.Append(msg, msg2);
  152. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  153. END InfoSS;
  154. (*** symbol lookup ***)
  155. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  156. **)
  157. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  158. VAR
  159. scope,baseScope: SyntaxTree.Scope;
  160. symbol, s: SyntaxTree.Symbol;
  161. ownerRecord,base: SyntaxTree.RecordType;
  162. BEGIN
  163. scope := inScope;
  164. symbol := NIL;
  165. WHILE (scope # NIL) & (symbol = NIL) DO
  166. symbol := scope.FindSymbol(name);
  167. s := NIL;
  168. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  169. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  170. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  171. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  172. END;
  173. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  174. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  175. ELSE
  176. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  177. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  178. symbol.MarkUsed;
  179. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  180. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  181. base := RecordBase(ownerRecord);
  182. IF (base # NIL) THEN
  183. baseScope := base.recordScope;
  184. symbol := Find(baseScope,name,FALSE);
  185. ELSE
  186. symbol := NIL;
  187. END;
  188. ELSE
  189. symbol := NIL;
  190. END;
  191. END;
  192. END;
  193. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  194. END;
  195. IF (symbol # NIL) THEN
  196. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  197. ASSERT(phase = DeclarationPhase);
  198. ResolveSymbol(symbol)
  199. END;
  200. symbol.MarkUsed;
  201. END;
  202. RETURN symbol
  203. END Find;
  204. (*------------ types -------------*)
  205. (** find type declaration with name qualifiedIdentifier and return resolved type
  206. - check qualified identifier prefix, set scope to module scope if appropriate
  207. - check suffix in scope
  208. **)
  209. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  210. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  211. BEGIN
  212. result := NIL;
  213. prevScope := currentScope;
  214. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  215. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  216. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  217. IF symbol(SyntaxTree.Import).module = NIL THEN
  218. Error(qualifiedIdentifier.position,"module not loaded");
  219. result := SyntaxTree.invalidType;
  220. symbol := NIL;
  221. ELSE
  222. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  223. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  224. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  225. IF VerboseErrorMessage THEN
  226. Printout.Info("scope", currentScope);
  227. Printout.Info("symbol", symbol);
  228. END;
  229. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  230. END;
  231. END;
  232. ELSE
  233. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  234. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  235. symbol := NIL;
  236. END;
  237. ELSE
  238. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  239. IF symbol = NIL THEN
  240. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  241. IF VerboseErrorMessage THEN
  242. Printout.Info("Qualident",qualifiedIdentifier);
  243. Printout.Info("in scope",currentScope) ;
  244. END;
  245. END;
  246. END;
  247. IF symbol = NIL THEN (* error already handled *)
  248. typeDeclaration := NIL;
  249. result := SyntaxTree.invalidType;
  250. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  251. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  252. typeDeclaration := NIL;
  253. result := SyntaxTree.invalidType;
  254. ELSE
  255. currentScope := symbol.scope;
  256. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  257. result := ResolveType(typeDeclaration.declaredType);
  258. symbol.MarkUsed;
  259. ASSERT(result # NIL);
  260. END;
  261. currentScope := prevScope;
  262. RETURN result
  263. END ResolveNamedType;
  264. (**
  265. check enumeration scope: enter symbols and check for duplicate names
  266. **)
  267. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: Basic.Integer);
  268. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: Basic.Integer; prevScope: SyntaxTree.Scope;
  269. BEGIN
  270. prevScope := currentScope;
  271. currentScope := x;
  272. e := x.firstConstant;
  273. nextHighest := highest;
  274. WHILE (e # NIL) DO
  275. Register(e,x,FALSE);
  276. IF SymbolNeedsResolution(e) THEN
  277. IF e.value # NIL THEN
  278. value := ConstantExpression(e.value);
  279. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  280. ELSE
  281. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  282. value.SetType(x.ownerEnumeration);
  283. END;
  284. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  285. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  286. IF nextHighest > highest THEN highest := nextHighest END;
  287. END;
  288. e.SetValue(value);
  289. CheckSymbolVisibility(e);
  290. e.SetType(x.ownerEnumeration);
  291. e.SetState(SyntaxTree.Resolved);
  292. END;
  293. e := e.nextConstant;
  294. END;
  295. currentScope := prevScope;
  296. END CheckEnumerationScope;
  297. (**
  298. resolve enumeration type: check enumeration scope
  299. **)
  300. PROCEDURE ResolveEnumerationType(x: SyntaxTree.EnumerationType);
  301. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  302. lowest, highest: Basic.Integer;
  303. BEGIN
  304. IF x.enumerationBase # NIL THEN
  305. position := x.enumerationBase.position;
  306. baseType := ResolveType(x.enumerationBase);
  307. resolved := baseType.resolved;
  308. baseScope := NIL;
  309. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  310. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  311. Error(position, "base type is no enumeration type");
  312. ELSE
  313. enumerationBase := resolved(SyntaxTree.EnumerationType);
  314. lowest := enumerationBase.rangeHighest+1;
  315. END;
  316. x.SetEnumerationBase(baseType);
  317. ELSE lowest := 0;
  318. END;
  319. highest := lowest-1;
  320. CheckEnumerationScope(x.enumerationScope, highest);
  321. x.SetRange(lowest, highest);
  322. x.SetState(SyntaxTree.Resolved);
  323. END ResolveEnumerationType;
  324. (**
  325. resolve qualified type
  326. - find and resolve named type and set resolved type
  327. **)
  328. PROCEDURE ResolveQualifiedType(x: SyntaxTree.QualifiedType);
  329. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  330. BEGIN
  331. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  332. x.SetResolved(type.resolved);
  333. x.SetState(SyntaxTree.Resolved);
  334. x.SetTypeDeclaration (typeDeclaration);
  335. (*
  336. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  337. x.SetResolved(SyntaxTree.invalidType);
  338. END;
  339. *)
  340. END ResolveQualifiedType;
  341. (**
  342. resolve array type
  343. - check base type
  344. - array of math array forbidden
  345. - static array of open array forbidden
  346. **)
  347. PROCEDURE ResolveArrayType(x: SyntaxTree.ArrayType);
  348. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  349. BEGIN
  350. x.SetArrayBase(ResolveType(x.arrayBase));
  351. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  352. arrayBase := x.arrayBase.resolved;
  353. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  354. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  355. pointerType.SetPointerBase(arrayBase);
  356. pointerType.SetHidden(TRUE);
  357. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  358. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  359. ELSE
  360. x.SetArrayBase(pointerType);
  361. END;
  362. END;
  363. IF x.length # NIL THEN
  364. variableAccessed := FALSE;
  365. e := ResolveExpression(x.length);
  366. IF (e.resolved = NIL) THEN
  367. IF variableAccessed THEN
  368. Error(e.position, "forbidden variable access");
  369. END;
  370. IF CheckSizeType(e) THEN
  371. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  372. END;
  373. ELSE
  374. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  375. END;
  376. END;
  377. IF arrayBase IS SyntaxTree.ArrayType THEN
  378. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  379. Error(x.position,"forbidden static array of dynamic array");
  380. END;
  381. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  382. Error(x.position,"forbidden array mixed form");
  383. END;
  384. x.SetHasPointers(arrayBase.hasPointers);
  385. x.SetState(SyntaxTree.Resolved);
  386. END ResolveArrayType;
  387. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  388. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  389. BEGIN
  390. module := currentScope.ownerModule;
  391. IF module.name=name THEN
  392. (* do nothing *)
  393. ELSE
  394. moduleScope := module.moduleScope;
  395. import := moduleScope.FindImport(name);
  396. IF import = NIL THEN
  397. import := SyntaxTree.NewImport(position,name,name,TRUE);
  398. moduleScope.AddImport(import);
  399. Register(import,moduleScope,FALSE);
  400. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  401. ResolveImport(import);
  402. ELSIF import.direct=FALSE THEN
  403. import.SetScope(module.moduleScope);
  404. import.SetDirect(TRUE);
  405. IF moduleScope.FindSymbol(import.name) = NIL THEN
  406. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  407. duplicate.SetContext(import.context);
  408. duplicate.SetModule(import.module);
  409. Register(duplicate,moduleScope,TRUE);
  410. ResolveImport(duplicate);
  411. END;
  412. END;
  413. import.MarkUsed
  414. END;
  415. END ImportModule;
  416. (**
  417. resolve math array type
  418. - check base type
  419. - open math array of array forbidden
  420. - math array of tensor forbidden
  421. - static array of open array forbidden
  422. **)
  423. PROCEDURE ResolveMathArrayType(x: SyntaxTree.MathArrayType);
  424. VAR arrayBase: SyntaxTree.Type; modifiers: SyntaxTree.Modifier; position: SyntaxTree.Position;
  425. BEGIN
  426. x.SetArrayBase(ResolveType(x.arrayBase));
  427. IF x.length # NIL THEN
  428. x.SetLength(ConstantIntegerGeq0(x.length));
  429. END;
  430. arrayBase := x.arrayBase;
  431. IF arrayBase # NIL THEN
  432. arrayBase := arrayBase.resolved;
  433. IF arrayBase = SyntaxTree.invalidType THEN
  434. (* error already handled *)
  435. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  436. Error(x.position,"forbidden array mixed form");
  437. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  438. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  439. Error(x.position,"forbidden Tensor Array mix")
  440. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  441. Error(x.position,"forbidden static array of dynamic array")
  442. END;
  443. END;
  444. IF x.form = SyntaxTree.Static THEN
  445. x.SetIncrement(system.SizeOf(arrayBase));
  446. END;
  447. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  448. END;
  449. modifiers := x.modifiers;
  450. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  451. CheckModifiers(modifiers, TRUE);
  452. x.SetState(SyntaxTree.Resolved);
  453. END ResolveMathArrayType;
  454. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  455. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  456. (1) Rec = RECORD ... END; Ptr <---> Rec
  457. Ptr = POINTER TO Rec; ^ |
  458. | |
  459. TypeDesc TypeDesc
  460. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  461. ^ /
  462. | /
  463. TypeDesc <-- /
  464. *)
  465. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  466. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  467. BEGIN
  468. Strings.IntToStr(x.position.start,number);
  469. COPY(prefix,name);
  470. Strings.Append(name,"@");
  471. Strings.Append(name,number);
  472. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  473. typeDeclaration.SetDeclaredType(x);
  474. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  475. x.SetTypeDeclaration(typeDeclaration);
  476. currentScope.AddTypeDeclaration(typeDeclaration);
  477. typeDeclaration.SetScope(currentScope);
  478. END AnonymousTypeDeclaration;
  479. (**
  480. deferred pointer type resolving
  481. - resolve base type
  482. - check that base type is a record or array type
  483. - if error then set base type to invalid type
  484. **)
  485. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  486. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  487. BEGIN
  488. ASSERT(type.pointerBase # NIL);
  489. position := type.pointerBase.position;
  490. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  491. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  492. (* not for pointers, a type is needed for the records only
  493. IF type.typeDeclaration = NIL THEN
  494. AnonymousTypeDeclaration(type);
  495. END;
  496. *)
  497. END;
  498. resolved := ResolveType(type.pointerBase);
  499. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  500. type.SetPointerBase(resolved);
  501. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  502. recordType := resolved.resolved(SyntaxTree.RecordType);
  503. IF recordType.isObject & (recordType.baseType # NIL) THEN
  504. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  505. Error(position, "base type of object must be a realtime object");
  506. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  507. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  508. END;
  509. END;
  510. END;
  511. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  512. Error(position, "realtime object contains references to non-realtime objects");
  513. END;
  514. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  515. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  516. Error(position, "forbidden unsafe at static array");
  517. ELS
  518. *)
  519. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  520. Error(position, "forbidden unsafe at multidimensional array");
  521. END;
  522. END;
  523. ELSE
  524. Error(position, "forbidden pointer base type");
  525. type.SetPointerBase(SyntaxTree.invalidType)
  526. END
  527. END FixPointerType;
  528. (**
  529. resolve pointer type
  530. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  531. **)
  532. PROCEDURE ResolvePointerType(x: SyntaxTree.PointerType);
  533. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  534. modifiers: SyntaxTree.Modifier; position: Position;
  535. BEGIN
  536. modifiers := x.modifiers;
  537. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  538. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  539. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  540. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  541. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  542. (* inheritance cycle check
  543. example:
  544. A=POINTER TO RECORD(B) END;
  545. B=POINTER TO RECORD(A) END;
  546. *)
  547. IF x.pointerBase IS SyntaxTree.RecordType THEN
  548. recordType := x.pointerBase(SyntaxTree.RecordType);
  549. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  550. recordBaseType := ResolveType(recordType.baseType);
  551. recordType.SetBaseType(recordBaseType);
  552. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  553. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  554. END;
  555. CheckModifiers(modifiers, TRUE);
  556. typeFixes.Add(x,currentScope);
  557. x.SetState(SyntaxTree.Resolved);
  558. END ResolvePointerType;
  559. (**
  560. resolve port type
  561. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  562. **)
  563. PROCEDURE ResolvePortType(x: SyntaxTree.PortType);
  564. VAR value: Basic.Integer;
  565. BEGIN
  566. x.SetCellsAreObjects(cellsAreObjects);
  567. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  568. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  569. x.SetSize(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  570. ELSE
  571. x.SetSize(system.SizeOf(system.longintType));
  572. END;
  573. x.SetState(SyntaxTree.Resolved);
  574. END ResolvePortType;
  575. (**
  576. deferred procedure type resolving
  577. - resolve return type
  578. - traverse and resolve parameters
  579. **)
  580. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  581. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  582. BEGIN
  583. resolved := ResolveType(procedureType.returnType);
  584. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  585. Error(procedureType.position,"forbidden open array return type");
  586. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  587. Error(procedureType.position,"procedure with return type does not return");
  588. END;
  589. procedureType.SetReturnType(resolved);
  590. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  591. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  592. parameter.SetType(procedureType.returnType);
  593. parameter.SetAccess(SyntaxTree.Hidden);
  594. parameter.SetUntraced(procedureType.hasUntracedReturn);
  595. ResolveParameter(parameter);
  596. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  597. END;
  598. (* process parameters *)
  599. parameter :=procedureType.firstParameter;
  600. WHILE (parameter # NIL) DO
  601. ResolveParameter(parameter);
  602. parameter := parameter.nextParameter;
  603. END;
  604. parameter := procedureType.selfParameter;
  605. IF parameter # NIL THEN
  606. ResolveParameter(parameter)
  607. END;
  608. END FixProcedureType;
  609. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  610. VAR prev,this: SyntaxTree.Modifier;
  611. BEGIN
  612. this := modifiers;prev := NIL;
  613. WHILE (this # NIL) & (this.identifier # name) DO
  614. prev := this; this := this.nextModifier;
  615. END;
  616. IF this # NIL THEN
  617. IF this.expression # NIL THEN
  618. Error(this.position,"unexpected expression");
  619. END;
  620. this.Resolved;
  621. position := this.position;
  622. RETURN TRUE
  623. ELSE
  624. RETURN FALSE
  625. END;
  626. END HasFlag;
  627. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: Basic.Integer): BOOLEAN;
  628. VAR prev,this: SyntaxTree.Modifier;
  629. BEGIN
  630. this := modifiers;prev := NIL;
  631. WHILE (this # NIL) & (this.identifier # name) DO
  632. prev := this; this := this.nextModifier;
  633. END;
  634. IF this # NIL THEN
  635. IF this.expression = NIL THEN
  636. Error(this.position,"expected expression value");
  637. ELSE
  638. this.SetExpression(ConstantExpression(this.expression));
  639. IF CheckIntegerValue(this.expression,value) THEN END;
  640. END;
  641. this.Resolved;
  642. position := this.position;
  643. RETURN TRUE
  644. ELSE RETURN FALSE
  645. END;
  646. END HasValue;
  647. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  648. VAR prev,this: SyntaxTree.Modifier;
  649. BEGIN
  650. this := modifiers;prev := NIL;
  651. WHILE (this # NIL) & (this.identifier # name) DO
  652. prev := this; this := this.nextModifier;
  653. END;
  654. IF this # NIL THEN
  655. IF this.expression = NIL THEN
  656. Error(this.position,"expected expression value");
  657. ELSE
  658. this.SetExpression(ConstantExpression(this.expression));
  659. IF CheckStringValue(this.expression,value) THEN END;
  660. END;
  661. this.Resolved;
  662. position := this.position;
  663. RETURN TRUE
  664. ELSE RETURN FALSE
  665. END;
  666. END HasStringValue;
  667. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  668. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  669. BEGIN
  670. IF cellsAreObjects THEN RETURN FALSE END;
  671. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  672. IF svalue = "A2" THEN
  673. RETURN TRUE
  674. END;
  675. END;
  676. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  677. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  678. END;
  679. RETURN FALSE;
  680. (*
  681. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  682. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  683. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  684. IF svalue[0] = "~" THEN
  685. Strings.TrimLeft(svalue, "~");
  686. IF svalue = backendName THEN
  687. RETURN TRUE;
  688. END;
  689. ELSIF svalue # backendName THEN
  690. RETURN TRUE;
  691. END;
  692. END;
  693. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  694. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  695. END;
  696. RETURN FALSE;
  697. *)
  698. END SkipImplementation;
  699. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  700. VAR this: SyntaxTree.Modifier;
  701. BEGIN
  702. this := modifiers;
  703. WHILE this # NIL DO
  704. IF ~this.resolved THEN
  705. IF checkUse THEN
  706. Error(this.position,"unexpected modifier");
  707. ELSE
  708. this.SetExpression(ResolveExpression(this.expression));
  709. this.Resolved;
  710. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  711. END;
  712. END;
  713. this := this.nextModifier
  714. END;
  715. END CheckModifiers;
  716. (**
  717. resolve procedure type
  718. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  719. **)
  720. PROCEDURE ResolveProcedureType(procedureType: SyntaxTree.ProcedureType);
  721. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position;
  722. BEGIN
  723. modifiers := procedureType.modifiers;
  724. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  725. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  726. procedureType.SetInterrupt(TRUE);
  727. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  728. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  729. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  730. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  731. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  732. Error(position, "undefined platform calling convention");
  733. ELSE
  734. procedureType.SetCallingConvention(system.platformCallingConvention);
  735. END;
  736. END;
  737. IF HasFlag(modifiers, Global.NameNoReturn,position) THEN procedureType.SetNoReturn(TRUE) END;
  738. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(LONGINT (value)) END; (* TODO: fix explicit integer truncation *)
  739. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  740. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  741. CheckModifiers(modifiers, TRUE);
  742. modifiers := procedureType.returnTypeModifiers;
  743. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  744. CheckModifiers(modifiers, TRUE);
  745. typeFixes.Add(procedureType,currentScope);
  746. procedureType.SetHasPointers(procedureType.isDelegate);
  747. procedureType.SetState(SyntaxTree.Resolved);
  748. END ResolveProcedureType;
  749. (** check and resolve record type
  750. - check base type: must be record, math array or array-structured object type
  751. - check declarations
  752. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  753. **)
  754. PROCEDURE ResolveRecordType(x: SyntaxTree.RecordType);
  755. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  756. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  757. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  758. hasPointers: BOOLEAN;
  759. modifiers: SyntaxTree.Modifier;
  760. value: Basic.Integer;
  761. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  762. BEGIN
  763. type := type.resolved;
  764. IF (type IS SyntaxTree.PointerType) &
  765. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  766. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  767. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  768. RETURN TRUE
  769. ELSE
  770. RETURN FALSE
  771. END;
  772. END IsPointerToRecord;
  773. BEGIN
  774. hasPointers := FALSE;
  775. modifiers := x.modifiers;
  776. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(LONGINT(value)*system.dataUnit) (* TODO: fix explicit integer truncation *)
  777. END;
  778. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  779. CheckModifiers(modifiers, TRUE);
  780. IF x.baseType # NIL THEN
  781. position := x.baseType.position;
  782. baseType := ResolveType(x.baseType);
  783. resolved := baseType.resolved;
  784. hasPointers := hasPointers OR resolved.hasPointers;
  785. IF x.isObject THEN (* object *)
  786. ASSERT(x.pointerType # NIL);
  787. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  788. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  789. baseType := NIL
  790. ELSIF IsPointerToRecord(resolved,recordType) THEN
  791. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  792. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  793. ELSE
  794. Error(position,"object does not extend pointer to record, object or math array ")
  795. END;
  796. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  797. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  798. ELSIF IsPointerToRecord(resolved,recordType) THEN
  799. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  800. ELSIF resolved IS SyntaxTree.RecordType THEN
  801. ELSE
  802. Error(position,"pointer to record does not extend pointer to record or record")
  803. END;
  804. ELSE
  805. IF resolved IS SyntaxTree.RecordType THEN
  806. ELSE
  807. Error(position,"record does not extend record")
  808. END;
  809. END;
  810. x.SetBaseType(baseType);
  811. IF x.Level() > 15 THEN
  812. Error(position, "record/object inheritance level too high");
  813. (* note:
  814. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  815. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  816. inheritance history of a type.
  817. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  818. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  819. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  820. *)
  821. END;
  822. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  823. Error(position, "invalid inheritance of disposable types");
  824. END;
  825. END;
  826. Declarations(x.recordScope, FALSE, {0});
  827. x.SetState(SyntaxTree.Resolved);
  828. Declarations(x.recordScope, FALSE, {1});
  829. (* computation of sizes and offsets skipped -> done in backend / system *)
  830. recordBase := x.GetBaseRecord();
  831. IF recordBase = NIL THEN numberMethods := 0
  832. ELSE numberMethods := recordBase.recordScope.numberMethods
  833. END;
  834. isRealtime := TRUE;
  835. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  836. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  837. END;
  838. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  839. WHILE symbol # NIL DO
  840. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  841. IF symbol IS SyntaxTree.Variable THEN
  842. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  843. END;
  844. IF symbol IS SyntaxTree.Procedure THEN
  845. procedure := symbol(SyntaxTree.Procedure);
  846. IF procedure.super # NIL THEN
  847. procedure.SetMethodNumber(procedure.super.methodNumber);
  848. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  849. procedure.SetMethodNumber(numberMethods);
  850. INC(numberMethods);
  851. END;
  852. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  853. Error(procedure.position,"realtime procedure in non-realtime object")
  854. END;
  855. END;
  856. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  857. Error(symbol.position,"non-realtime symbol in realtime object")
  858. END;
  859. symbol := symbol.nextSymbol;
  860. END;
  861. IF isRealtime THEN x.SetRealtime(TRUE) END;
  862. x.recordScope.SetNumberMethods(numberMethods);
  863. (* TODO: is this needed anymore? *)
  864. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  865. Error(x.position,"object extends a record")
  866. END;
  867. IF ~x.isAbstract THEN
  868. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  869. Error(x.position, "non-abstract object contains abstract procedure");
  870. END;
  871. ELSE
  872. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  873. Error(x.position, "abstract object does not contain an abstract method");
  874. END;
  875. END;
  876. IF (x.typeDeclaration = NIL) THEN
  877. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  878. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  879. (*
  880. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  881. AnonymousTypeDeclaration(x,name);
  882. *)
  883. ELSE
  884. AnonymousTypeDeclaration(x,"Anonymous");
  885. END;
  886. END;
  887. x.SetHasPointers(hasPointers);
  888. x.SetState(SyntaxTree.Resolved);
  889. END ResolveRecordType;
  890. (** check and resolve cell type
  891. - check base type: must be cell
  892. - check declarations
  893. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  894. **)
  895. PROCEDURE ResolveCellType(x: SyntaxTree.CellType);
  896. VAR
  897. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  898. modifier: SyntaxTree.Modifier; position: Position; value: Basic.Integer; isEngine: BOOLEAN; property: SyntaxTree.Property;
  899. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  900. recordBase: SyntaxTree.RecordType;
  901. numberMethods: LONGINT;
  902. int: Basic.Integer;
  903. real: LONGREAL;
  904. bool: BOOLEAN;
  905. set: Basic.Set;
  906. v: SyntaxTree.Expression;
  907. str: Scanner.StringType;
  908. atype: SyntaxTree.ArrayType;
  909. prev: SyntaxTree.Scope;
  910. BEGIN
  911. recordBase := NIL;
  912. IF cellsAreObjects THEN
  913. IF x.baseType = NIL THEN
  914. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  915. ImportModule(qualifiedIdentifier.prefix, x.position);
  916. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  917. x.SetBaseType(ResolveType(x.baseType));
  918. recordBase := x.GetBaseRecord();
  919. IF recordBase = NIL THEN
  920. Error(x.position,"ActiveCellsRuntime.Cell not present");
  921. END;
  922. ELSE
  923. x.SetBaseType(ResolveType(x.baseType));
  924. END;
  925. ELSE
  926. x.SetBaseType(ResolveType(x.baseType));
  927. END;
  928. IF recordBase = NIL THEN numberMethods := 0
  929. ELSE numberMethods := recordBase.recordScope.numberMethods
  930. END;
  931. modifier := x.modifiers;
  932. (*IF ~x.isCellNet THEN*)
  933. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  934. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  935. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  936. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  937. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  938. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  939. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  940. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  941. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  942. symbol := system.activeCellsCapabilities;
  943. WHILE symbol # NIL DO
  944. IF HasFlag(modifier, symbol.name, position) THEN END;
  945. symbol := symbol.nextSymbol;
  946. END;
  947. modifier := x.modifiers;
  948. WHILE (modifier # NIL) DO
  949. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  950. IF modifier.expression # NIL THEN
  951. v := ConstantExpression(modifier.expression);
  952. property.SetValue(v);
  953. IF IsIntegerValue(modifier.expression, int) THEN
  954. (*property.SetValue(modifier.expression);*)
  955. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  956. property.SetType(system.longintType);
  957. ELSIF IsRealValue(modifier.expression, real) THEN
  958. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  959. property.SetType(system.longrealType);
  960. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  961. property.SetType(system.booleanType);
  962. ELSIF IsSetValue(modifier.expression, set) THEN
  963. property.SetType(system.setType);
  964. ELSIF IsStringValue(modifier.expression, str) THEN
  965. (*property.SetValue(modifier.expression);*)
  966. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  967. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  968. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  969. property.SetType(atype);
  970. ELSE
  971. Error(modifier.position, "unsupported property type");
  972. END;
  973. ELSE (* flag property *)
  974. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  975. property.SetType(system.booleanType);
  976. END;
  977. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  978. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  979. x.AddProperty(property);
  980. modifier := modifier.nextModifier;
  981. END;
  982. CheckModifiers(modifier, FALSE);
  983. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  984. (* process parameters *)
  985. prev := currentScope;
  986. currentScope := x.cellScope;
  987. parameter :=x.firstParameter;
  988. WHILE (parameter # NIL) DO
  989. ResolveParameter(parameter);
  990. type := parameter.type.resolved;
  991. IF ~(type IS SyntaxTree.PortType) THEN
  992. WHILE IsStaticArray(type, type, len) DO
  993. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  994. END;
  995. WHILE IsDynamicArray(type, type) DO
  996. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  997. END;
  998. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  999. Error(parameter.position, "invalid type, must be port or static array of port ");
  1000. END;
  1001. END;
  1002. parameter := parameter.nextParameter;
  1003. END;
  1004. currentScope := prev;
  1005. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1006. WHILE symbol # NIL DO
  1007. IF symbol IS SyntaxTree.Variable THEN
  1008. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1009. END;
  1010. symbol := symbol.nextSymbol;
  1011. END;
  1012. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1013. IF (x.typeDeclaration = NIL) THEN
  1014. AnonymousTypeDeclaration(x,"Anonymous");
  1015. END;
  1016. x.SetState(SyntaxTree.Resolved);
  1017. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1018. Warning(x.position, "Forbidden empty Body.");
  1019. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1020. Warning(x.position, "Non-empty body for an engine?");
  1021. END;
  1022. END ResolveCellType;
  1023. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  1024. If node is currently being resolved then emit a cyclic definition error.
  1025. Return TRUE only if node is fully resolved.
  1026. **)
  1027. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  1028. VAR result: BOOLEAN;
  1029. BEGIN
  1030. IF SyntaxTree.Resolved IN x.state THEN
  1031. result := FALSE
  1032. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  1033. Error(x.position,"cyclic definition");
  1034. result := FALSE;
  1035. ELSE
  1036. result := TRUE;
  1037. x.SetState(SyntaxTree.BeingResolved)
  1038. END;
  1039. RETURN result
  1040. END TypeNeedsResolution;
  1041. PROCEDURE ResolveType*(x: SyntaxTree.Type): SyntaxTree.Type;
  1042. BEGIN
  1043. IF (x = NIL) OR (x = SyntaxTree.invalidType) THEN RETURN x
  1044. ELSIF TypeNeedsResolution(x) THEN
  1045. WITH x: SyntaxTree.ProcedureType DO ResolveProcedureType(x)
  1046. |SyntaxTree.CellType DO ResolveCellType(x)
  1047. |SyntaxTree.RecordType DO ResolveRecordType(x)
  1048. |SyntaxTree.PortType DO ResolvePortType(x)
  1049. |SyntaxTree.PointerType DO ResolvePointerType(x)
  1050. |SyntaxTree.MathArrayType DO ResolveMathArrayType(x)
  1051. |SyntaxTree.ArrayType DO ResolveArrayType(x)
  1052. |SyntaxTree.RangeType DO x.SetState(SyntaxTree.Resolved)
  1053. |SyntaxTree.EnumerationType DO ResolveEnumerationType(x)
  1054. |SyntaxTree.StringType DO x.SetState(SyntaxTree.Resolved)
  1055. |SyntaxTree.QualifiedType DO ResolveQualifiedType(x)
  1056. |SyntaxTree.BasicType DO x.SetState(SyntaxTree.Resolved)
  1057. END; (* no other case may remain *)
  1058. END;
  1059. IF SyntaxTree.Resolved IN x.state THEN
  1060. RETURN x
  1061. ELSE
  1062. RETURN SyntaxTree.invalidType
  1063. END;
  1064. END ResolveType;
  1065. (** resolve all pending types (late resolving).
  1066. - type fixes are resolved at the end of the declaration phase
  1067. - type fixes may imply new type fixes that are also entered at the end of the list
  1068. **)
  1069. PROCEDURE FixTypes;
  1070. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1071. BEGIN
  1072. prevScope := currentScope;
  1073. p := typeFixes.Get(currentScope);
  1074. WHILE p # NIL DO
  1075. ASSERT(currentScope # NIL);
  1076. ASSERT(p IS SyntaxTree.Type);
  1077. IF p IS SyntaxTree.PointerType THEN
  1078. FixPointerType(p(SyntaxTree.PointerType))
  1079. ELSIF p IS SyntaxTree.ProcedureType THEN
  1080. FixProcedureType(p(SyntaxTree.ProcedureType))
  1081. ELSE
  1082. HALT(100);
  1083. END;
  1084. p := typeFixes.Get(currentScope);
  1085. END;
  1086. currentScope :=prevScope;
  1087. END FixTypes;
  1088. (*** compatibility rules ***)
  1089. (**
  1090. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1091. **)
  1092. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1093. VAR result: SyntaxTree.Type;
  1094. BEGIN
  1095. result := SyntaxTree.invalidType;
  1096. IF type = NIL THEN Error(position, "expression of type NIL");
  1097. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1098. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1099. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1100. ELSE result := type.resolved
  1101. END;
  1102. RETURN result
  1103. END RegularType;
  1104. (** returns signature compatibility of procedure types this and to
  1105. - if not compatible then error is reported
  1106. - compatibility means type equality
  1107. **)
  1108. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1109. VAR result: BOOLEAN;
  1110. BEGIN
  1111. result := SameType(to,this);
  1112. IF ~result THEN
  1113. Error(position, "signature incompatible");
  1114. IF VerboseErrorMessage THEN
  1115. Printout.Info("this",this);
  1116. Printout.Info("to",to);
  1117. END;
  1118. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1119. Error(position, "signature incompatible: realtime flag must be inherited");
  1120. END;
  1121. RETURN result
  1122. END SignatureCompatible;
  1123. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1124. - for var parameters compatibility means same type except for
  1125. - formal is of open array of system byte
  1126. - formal is of record type
  1127. - formal is of open array type
  1128. - formal is of open math array type
  1129. - for value parameters compatibllity means assignment compatibility except for
  1130. - formal is of open array type
  1131. if compatible the return true else report error and return false
  1132. **)
  1133. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1134. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1135. BEGIN
  1136. formalType := RegularType(formal.position,formal.type);
  1137. actualType := RegularType(actual.position,actual.type);
  1138. error := FALSE;
  1139. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1140. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1141. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1142. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1143. END;
  1144. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1145. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1146. ELSIF ~IsVariable(actual) THEN
  1147. result := FALSE; error := TRUE;
  1148. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1149. Error(actual.position,"not a variable: no operator for writing");
  1150. ELSE
  1151. Error(actual.position,"is not a variable");
  1152. END;
  1153. IF VerboseErrorMessage THEN
  1154. Printout.Info("actual",actual);
  1155. Printout.Info("formal",formal);
  1156. END;
  1157. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1158. result := CompatibleTo(system,actualType,formalType);
  1159. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1160. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1161. ELSIF IsUnsafePointer(formalType) & IsUnsafePointer(actualType) THEN
  1162. result := TRUE;
  1163. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1164. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1165. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1166. Error(actual.position,"incompatible non-static actual type");
  1167. END;
  1168. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1169. Error(actual.position,"incompatible tensor (use a range expression)");
  1170. END;
  1171. ELSE
  1172. result := SameType(actualType,formalType)
  1173. END
  1174. ELSE
  1175. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1176. actualType := system.characterType;
  1177. END;
  1178. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1179. result := TRUE; (* special rule for WINAPI parameters *)
  1180. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1181. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1182. ELSE
  1183. result := CompatibleTo(system,actualType,formalType);
  1184. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1185. Error(actual.position,"incompatible non-static actual type");
  1186. END;
  1187. END;
  1188. END;
  1189. IF ~result & ~error THEN
  1190. Error(actual.position,"incompatible parameter");
  1191. IF VerboseErrorMessage THEN
  1192. Printout.Info("actual",actual);
  1193. Printout.Info("formal",formal);
  1194. END;
  1195. END;
  1196. RETURN result
  1197. END ParameterCompatible;
  1198. (** check compatibility for expressions of the form left := right
  1199. - if compatible then return true else error report and return false
  1200. - check if left is variable
  1201. - check compatiblity
  1202. **)
  1203. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1204. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1205. BEGIN
  1206. result := FALSE;
  1207. leftType := RegularType(left.position,left.type);
  1208. rightType := RegularType(right.position,right.type);
  1209. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1210. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1211. END;
  1212. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1213. (* error already handled *)
  1214. result := TRUE;
  1215. ELSIF ~IsVariable(left) THEN
  1216. Error(left.position,"is not a variable");
  1217. IF VerboseErrorMessage THEN
  1218. Printout.Info("left",left);
  1219. Printout.Info("right",right);
  1220. END;
  1221. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1222. result := TRUE;
  1223. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1224. result := TRUE
  1225. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1226. Error(left.position,"incompatible assignment");
  1227. IF VerboseErrorMessage THEN
  1228. Printout.Info("left",left);
  1229. Printout.Info("right",right);
  1230. END;
  1231. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1232. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1233. Error(right.position,"forbidden assignment of a nested procedure");
  1234. ELSE
  1235. result := TRUE
  1236. END;
  1237. RETURN result
  1238. END AssignmentCompatible;
  1239. (*** expressions ***)
  1240. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1241. - check all elements on integer type
  1242. - if element range is constant, then check lower and upper bound
  1243. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1244. if an error occurs then report error and return invalidExpression
  1245. **)
  1246. PROCEDURE ResolveSet(set: SyntaxTree.Set): SyntaxTree.Expression;
  1247. VAR
  1248. i: LONGINT;
  1249. element: SyntaxTree.Expression;
  1250. constant: BOOLEAN;
  1251. elements: SyntaxTree.ExpressionList;
  1252. s: Basic.Set;
  1253. result: SyntaxTree.Expression;
  1254. value: SyntaxTree.Value;
  1255. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1256. VAR
  1257. left, right: SyntaxTree.Expression;
  1258. elementResult: SyntaxTree.Expression;
  1259. leftInteger, rightInteger: Basic.Integer;
  1260. BEGIN
  1261. (* set context of range *)
  1262. IF element IS SyntaxTree.RangeExpression THEN
  1263. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1264. END;
  1265. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1266. IF elementResult = SyntaxTree.invalidExpression THEN
  1267. (* error already reported *)
  1268. constant := FALSE
  1269. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1270. (* the element is a range expression *)
  1271. (* extract left and right hand side of range *)
  1272. left := elementResult(SyntaxTree.RangeExpression).first;
  1273. right := elementResult(SyntaxTree.RangeExpression).last;
  1274. (* guaranteed by VisitRangeExpression: *)
  1275. ASSERT((left # NIL) & (right # NIL));
  1276. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1277. ELSE
  1278. (* the element is not a range expression *)
  1279. (* check type and add conversion if needed *)
  1280. IF IsIntegerType(elementResult.type.resolved) THEN
  1281. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1282. ELSE
  1283. Error(elementResult.position, "non integer element in set");
  1284. elementResult := SyntaxTree.invalidExpression;
  1285. constant := FALSE
  1286. END;
  1287. left := elementResult;
  1288. right := elementResult
  1289. END;
  1290. IF elementResult # SyntaxTree.invalidExpression THEN
  1291. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1292. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1293. Error(left.position,"not allowed set integer value");
  1294. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1295. Error(right.position,"not allowed set integer value");
  1296. END
  1297. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1298. Error(right.position,"not allowed set integer value");
  1299. ELSE
  1300. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1301. s := {};
  1302. ELSE
  1303. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1304. IF leftInteger < 0 THEN leftInteger := 0 END;
  1305. (*!!!!!!!!! this is a hack !!!!!!! *)
  1306. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1307. extends the range x..31 to x..63 !!!!!! *)
  1308. s := s + {leftInteger..rightInteger};
  1309. END;
  1310. END;
  1311. ELSE
  1312. constant := FALSE;
  1313. END
  1314. END;
  1315. RETURN elementResult
  1316. END CheckElement;
  1317. BEGIN
  1318. result := set; constant := TRUE; s := {}; elements := set.elements;
  1319. IF elements # NIL THEN
  1320. FOR i := 0 TO elements.Length()-1 DO
  1321. element := elements.GetExpression(i);
  1322. element := CheckElement(element);
  1323. IF element = SyntaxTree.invalidExpression THEN
  1324. result := SyntaxTree.invalidExpression
  1325. END;
  1326. elements.SetExpression(i,element);
  1327. END;
  1328. END;
  1329. IF constant THEN
  1330. value := Global.NewSetValue(system,set.position,s);
  1331. result.SetResolved(value);
  1332. result.SetType(value.type);
  1333. ELSE
  1334. result.SetType(system.setType);
  1335. END;
  1336. (* optimization possible
  1337. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1338. left this to the programmer...
  1339. *)
  1340. RETURN result;
  1341. END ResolveSet;
  1342. (*
  1343. old variant: quite generic but needs better conversion handling, do this?
  1344. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1345. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1346. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1347. BEGIN
  1348. type := NIL;
  1349. numberElements := x.elements.Length();
  1350. FOR i := 0 TO numberElements-1 DO
  1351. expression := x.elements.GetExpression(i);
  1352. position := expression.position;
  1353. expression := ResolveExpression(x.elements.GetExpression(i));
  1354. x.elements.SetExpression(i,de);
  1355. IF type = NIL THEN
  1356. type := expression.type;
  1357. ELSIF CompatibleTo(system,expression.type,type) THEN
  1358. (* ok *)
  1359. ELSIF CompatibleTo(system,type,expression.type) THEN
  1360. type := expression.type
  1361. ELSE
  1362. Error(expression.position, "incompatible element types");
  1363. type := SyntaxTree.invalidType;
  1364. END;
  1365. END;
  1366. isValue := TRUE;
  1367. FOR i := 0 TO numberElements-1 DO
  1368. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1369. x.elements.SetExpression(i,expression);
  1370. isValue := isValue & (expression.resolved # NIL);
  1371. END;
  1372. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1373. arrayType.SetArrayBase(type);
  1374. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1375. IF isValue THEN
  1376. value := SyntaxTree.NewMathArrayValue(position);
  1377. value.SetElements(x.elements);
  1378. x.SetResolved(value);
  1379. END;
  1380. x.SetType(arrayType);
  1381. resolvedExpression := x;
  1382. END VisitMathArrayExpression;
  1383. *)
  1384. PROCEDURE ResolveMathArrayExpression(x: SyntaxTree.MathArrayExpression): SyntaxTree.Expression;
  1385. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1386. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1387. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1388. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1389. BEGIN
  1390. numberElements := x.elements.Length();
  1391. FOR i := 0 TO numberElements-1 DO
  1392. expression := x.elements.GetExpression(i);
  1393. IF expression IS SyntaxTree.MathArrayExpression THEN
  1394. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1395. ELSE
  1396. position := expression.position;
  1397. expression := ResolveExpression(x.elements.GetExpression(i));
  1398. x.elements.SetExpression(i,expression);
  1399. IF type = NIL THEN
  1400. type := expression.type;
  1401. ELSIF CompatibleTo(system,expression.type,type) THEN
  1402. (* ok *)
  1403. ELSIF CompatibleTo(system,type,expression.type) THEN
  1404. type := expression.type
  1405. ELSE
  1406. Error(expression.position, "incompatible element types");
  1407. type := SyntaxTree.invalidType;
  1408. END;
  1409. END;
  1410. END;
  1411. END RecursivelyFindType;
  1412. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1413. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1414. BEGIN
  1415. numberElements := x.elements.Length();
  1416. FOR i := 0 TO numberElements-1 DO
  1417. expression := x.elements.GetExpression(i);
  1418. IF expression IS SyntaxTree.MathArrayExpression THEN
  1419. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1420. ELSE
  1421. position := expression.position;
  1422. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1423. x.elements.SetExpression(i,expression);
  1424. isValue := isValue & (expression.resolved # NIL);
  1425. END;
  1426. END;
  1427. END RecursivelySetExpression;
  1428. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1429. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1430. arrayType: SyntaxTree.MathArrayType;
  1431. BEGIN
  1432. numberElements := x.elements.Length();
  1433. baseType := NIL;
  1434. gsize := 0;
  1435. FOR i := 0 TO numberElements-1 DO
  1436. expression := x.elements.GetExpression(i);
  1437. IF expression IS SyntaxTree.MathArrayExpression THEN
  1438. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1439. IF i=0 THEN
  1440. gsize := size;
  1441. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1442. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1443. ELSE expression.SetType(baseType)
  1444. END;
  1445. ELSIF baseType = NIL THEN baseType := type;
  1446. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1447. END;
  1448. END;
  1449. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1450. arrayType.SetArrayBase(baseType);
  1451. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1452. RETURN ResolveType(arrayType);
  1453. END RecursivelySetType;
  1454. BEGIN
  1455. type := NIL;
  1456. RecursivelyFindType(x);
  1457. isValue := TRUE;
  1458. RecursivelySetExpression(x);
  1459. arrayType := RecursivelySetType(x);
  1460. x.SetType(arrayType);
  1461. IF isValue THEN
  1462. value := SyntaxTree.NewMathArrayValue(x.position);
  1463. value.SetArray(x);
  1464. x.SetResolved(value);
  1465. value.SetType(arrayType);
  1466. END;
  1467. x.SetType(arrayType);
  1468. RETURN x;
  1469. END ResolveMathArrayExpression;
  1470. (** check and resolve unary expression **)
  1471. PROCEDURE ResolveUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression): SyntaxTree.Expression;
  1472. VAR
  1473. left: SyntaxTree.Expression;
  1474. int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; operator: LONGINT;
  1475. bool: BOOLEAN;
  1476. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1477. value: SyntaxTree.Value;
  1478. BEGIN
  1479. type := SyntaxTree.invalidType;
  1480. left := ResolveExpression(unaryExpression.left);
  1481. unaryExpression.SetLeft(left);
  1482. operator := unaryExpression.operator;
  1483. result := unaryExpression;
  1484. IF ~system.operatorDefined[operator] THEN
  1485. Error(left.position,"Operator Not Defined");
  1486. RETURN SyntaxTree.invalidExpression;
  1487. ELSIF left.type = NIL THEN
  1488. Error(left.position,"Invalid Nil Argument in Unary Expression");
  1489. RETURN SyntaxTree.invalidExpression;
  1490. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1491. RETURN SyntaxTree.invalidExpression;
  1492. END;
  1493. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1494. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1495. END;
  1496. IF operatorCall # NIL THEN
  1497. result := operatorCall;
  1498. type := operatorCall.type;
  1499. (* admissible operators
  1500. Minus number, set
  1501. Not boolean
  1502. *)
  1503. ELSE
  1504. CASE unaryExpression.operator OF
  1505. |Scanner.Minus:
  1506. IF IsIntegerType(left.type.resolved) THEN
  1507. IF left.resolved # NIL THEN
  1508. int := -left.resolved(SyntaxTree.IntegerValue).value;
  1509. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1510. result.SetResolved(value);
  1511. type := Global.GetIntegerType(system,int);
  1512. value.SetType(type);
  1513. ELSE
  1514. type := left.type
  1515. END
  1516. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1517. IF IsRealValue(left,real) THEN
  1518. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  1519. result.SetResolved(value);
  1520. type := left.type;
  1521. value.SetType(type);
  1522. ELSE
  1523. type := left.type;
  1524. END;
  1525. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1526. IF IsSetValue(left,set) THEN
  1527. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1528. result.SetResolved(value);
  1529. type := Global.GetSetType(system,-set);
  1530. value.SetType(type);
  1531. ELSE
  1532. type := left.type;
  1533. END;
  1534. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1535. IF IsComplexValue(left, real, imaginary) THEN
  1536. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  1537. result.SetResolved(value);
  1538. type := left.type;
  1539. value.SetType(type);
  1540. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1541. ELSE
  1542. type := left.type;
  1543. END
  1544. ELSE
  1545. Error(left.position,"unary operator not applicable");
  1546. END;
  1547. |Scanner.Not:
  1548. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1549. IF IsBooleanValue(left,bool) THEN
  1550. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1551. result.SetResolved(value);
  1552. type := system.booleanType;
  1553. value.SetType(type);
  1554. ELSE
  1555. type := system.booleanType;
  1556. END;
  1557. ELSE
  1558. Error(left.position,"unary operator not applicable");
  1559. END;
  1560. |Scanner.Plus:
  1561. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  1562. result := left; type := left.type;
  1563. ELSE
  1564. Error(left.position,"unary operator not applicable");
  1565. END;
  1566. (* ADDRESS OF *)
  1567. |Scanner.Address:
  1568. IF HasAddress(left) THEN
  1569. type := system.addressType;
  1570. ELSE
  1571. type := SyntaxTree.invalidType;
  1572. Error(left.position,"has no address");
  1573. Printout.Info("par", left);
  1574. END;
  1575. (* SIZE OF *)
  1576. |Scanner.Size:
  1577. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  1578. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  1579. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  1580. value := SyntaxTree.NewIntegerValue(left.position, int);
  1581. result.SetResolved(value);
  1582. type := Global.GetIntegerType(system,int);
  1583. value.SetType(type)
  1584. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  1585. ELSE
  1586. (* for variables, system sizeof could represent the physically occupied size
  1587. determined via the type descriptor, implement that ? *)
  1588. Error(left.position,"is not a type symbol");
  1589. END
  1590. (* ALIAS OF *)
  1591. |Scanner.Alias:
  1592. type := left.type.resolved;
  1593. IF ~(type IS SyntaxTree.MathArrayType) THEN
  1594. type := SyntaxTree.invalidType;
  1595. Error(left.position,"alias on non math array type");
  1596. END;
  1597. ELSE
  1598. Error(left.position,"unary operator not defined");
  1599. END;
  1600. END;
  1601. result.SetType(type);
  1602. RETURN result
  1603. END ResolveUnaryExpression;
  1604. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  1605. VAR
  1606. result: SyntaxTree.Expression;
  1607. array: SyntaxTree.MathArrayExpression;
  1608. value: SyntaxTree.MathArrayValue;
  1609. isValue: BOOLEAN;
  1610. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  1611. BEGIN
  1612. type := type.resolved;
  1613. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  1614. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  1615. END;
  1616. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  1617. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  1618. END;
  1619. RETURN type
  1620. END BaseType;
  1621. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  1622. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  1623. BEGIN
  1624. numberElements := x.elements.Length();
  1625. FOR i := 0 TO numberElements-1 DO
  1626. expression := x.elements.GetExpression(i);
  1627. IF expression IS SyntaxTree.MathArrayExpression THEN
  1628. array := SyntaxTree.NewMathArrayExpression(position);
  1629. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  1630. expression := array;
  1631. ELSE
  1632. position := expression.position;
  1633. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1634. isValue := isValue & (expression.resolved # NIL);
  1635. END;
  1636. to.elements.AddExpression(expression);
  1637. END;
  1638. END RecursivelyConvert;
  1639. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1640. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1641. arrayType: SyntaxTree.MathArrayType;
  1642. BEGIN
  1643. numberElements := x.elements.Length();
  1644. baseType := NIL;
  1645. gsize := 0;
  1646. FOR i := 0 TO numberElements-1 DO
  1647. expression := x.elements.GetExpression(i);
  1648. IF expression IS SyntaxTree.MathArrayExpression THEN
  1649. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1650. IF i=0 THEN
  1651. gsize := size;
  1652. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1653. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1654. ELSE expression.SetType(baseType)
  1655. END;
  1656. ELSIF baseType = NIL THEN baseType := type;
  1657. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1658. END;
  1659. END;
  1660. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1661. arrayType.SetArrayBase(baseType);
  1662. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1663. RETURN ResolveType(arrayType);
  1664. END RecursivelySetType;
  1665. BEGIN
  1666. result := SyntaxTree.invalidExpression;
  1667. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  1668. result := expression (* do not convert *)
  1669. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  1670. isValue := TRUE;
  1671. type := BaseType(type);
  1672. array := SyntaxTree.NewMathArrayExpression(expression.position);
  1673. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  1674. value := SyntaxTree.NewMathArrayValue(array.position);
  1675. value.SetArray(array);
  1676. value.SetType(RecursivelySetType(array));
  1677. result := value;
  1678. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  1679. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  1680. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  1681. IF result = NIL THEN
  1682. result := SyntaxTree.invalidExpression;
  1683. Error(position, "incompatible conversion");
  1684. IF VerboseErrorMessage THEN
  1685. Printout.Info("expression",expression);
  1686. Printout.Info("type",type);
  1687. END;
  1688. END;
  1689. END;
  1690. RETURN result
  1691. END MathArrayConversion;
  1692. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  1693. VAR result: SyntaxTree.Expression; int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; char: CHAR; string: Scanner.StringType;
  1694. BEGIN
  1695. result := expression; type := type.resolved;
  1696. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  1697. (* skip, no conversion *)
  1698. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  1699. int := expression(SyntaxTree.IntegerValue).value;
  1700. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  1701. int := Global.ConvertSigned(int,system.SizeOf(type));
  1702. result := SyntaxTree.NewIntegerValue(position,int);
  1703. result.SetType(type);
  1704. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  1705. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  1706. result := SyntaxTree.NewIntegerValue(position,int);
  1707. result.SetType(type);
  1708. ELSIF (type IS SyntaxTree.FloatType) THEN
  1709. result := SyntaxTree.NewRealValue(expression.position,int);
  1710. result.SetType(type);
  1711. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1712. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  1713. result.SetType(type);
  1714. ELSIF (type IS SyntaxTree.SetType) THEN
  1715. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,int));
  1716. result.SetType(type);
  1717. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  1718. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  1719. result.SetType(type);
  1720. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  1721. result := SyntaxTree.NewEnumerationValue(expression.position,int);
  1722. result.SetType(type);
  1723. ELSIF (type IS SyntaxTree.PortType) THEN
  1724. result := ConvertValue(position, expression, system.integerType);
  1725. ELSE
  1726. Error(position, "integer value cannot be converted");
  1727. result := SyntaxTree.invalidExpression;
  1728. IF VerboseErrorMessage THEN
  1729. Printout.Info("expression",expression);
  1730. Printout.Info("type",type);
  1731. END;
  1732. END;
  1733. ELSIF IsRealValue(expression,real) THEN
  1734. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  1735. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  1736. result := SyntaxTree.NewIntegerValue(expression.position,int);
  1737. result.SetType(type);
  1738. ELSIF (type IS SyntaxTree.IntegerType) THEN
  1739. int := ENTIERH(real);
  1740. result := SyntaxTree.NewIntegerValue(expression.position,int);
  1741. result.SetType(type);
  1742. ELSIF (type IS SyntaxTree.FloatType) THEN
  1743. result := SyntaxTree.NewRealValue(position,real);
  1744. result.SetType(type);
  1745. ELSIF (type IS SyntaxTree.ComplexType) THEN
  1746. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  1747. result.SetType(type);
  1748. result(SyntaxTree.ComplexValue).UpdateSubtype;
  1749. ELSIF (type IS SyntaxTree.PortType) THEN
  1750. result := ConvertValue(position, expression, system.integerType);
  1751. ELSE
  1752. Error(position, "real value cannot be converted");
  1753. result := SyntaxTree.invalidExpression;
  1754. END
  1755. ELSIF IsComplexValue(expression, real, imaginary) THEN
  1756. IF (type IS SyntaxTree.ComplexType) THEN
  1757. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  1758. result.SetType(type);
  1759. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1760. ELSE
  1761. Error(position, "complex value cannot be converted");
  1762. result := SyntaxTree.invalidExpression;
  1763. END
  1764. ELSIF IsSetValue(expression,set) THEN
  1765. IF (type IS SyntaxTree.IntegerType) THEN
  1766. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,set));
  1767. result.SetType(type);
  1768. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(Basic.Integer,set)) *)
  1769. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  1770. result.SetType(type);
  1771. ELSIF (type IS SyntaxTree.PortType) THEN
  1772. result := ConvertValue(position, expression, system.integerType);
  1773. ELSIF (type IS SyntaxTree.SetType) THEN
  1774. set := Global.ConvertSet(set,system.SizeOf(type));
  1775. result := SyntaxTree.NewSetValue(expression.position,set);
  1776. result.SetType(type);
  1777. ELSE
  1778. Error(position, "set value cannot be converted");
  1779. result := SyntaxTree.invalidExpression;
  1780. END;
  1781. ELSIF IsStringValue(expression,string) THEN
  1782. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  1783. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  1784. result.SetType(type);
  1785. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  1786. ELSE
  1787. Error(position, "string value cannot be converted");
  1788. result := SyntaxTree.invalidExpression;
  1789. END;
  1790. ELSIF IsCharacterValue(expression,char) THEN
  1791. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  1792. string[0] := char; string[1] := 0X;
  1793. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  1794. result := SyntaxTree.NewStringValue(expression.position,string);
  1795. result.SetType(type);
  1796. ELSIF (type IS SyntaxTree.ByteType) THEN
  1797. (* do not simply set the new type as this could invalidate types of constants *)
  1798. result := SyntaxTree.NewCharacterValue(expression.position,char);
  1799. result.SetType(type)
  1800. ELSIF (type IS SyntaxTree.IntegerType) THEN
  1801. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,char));
  1802. result.SetType(type);
  1803. ELSIF (type IS SyntaxTree.SetType) THEN
  1804. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,Basic.Integer(ORD(char))));
  1805. result.SetType(type);
  1806. ELSIF (type IS SyntaxTree.CharacterType) THEN
  1807. result := SyntaxTree.NewCharacterValue(expression.position,char);
  1808. result.SetType(type);
  1809. ELSIF (type IS SyntaxTree.PortType) THEN
  1810. result := ConvertValue(position, expression, system.integerType);
  1811. ELSE
  1812. Error(position, "character value cannot be converted");
  1813. result := SyntaxTree.invalidExpression;
  1814. END;
  1815. ELSIF expression IS SyntaxTree.NilValue THEN
  1816. IF type IS SyntaxTree.AddressType THEN
  1817. result := SyntaxTree.NewIntegerValue(position,0);
  1818. result.SetType(type);
  1819. ELSE
  1820. result := expression;
  1821. END;
  1822. (* nothing to be done *)
  1823. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  1824. result := MathArrayConversion(position, expression,type);
  1825. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  1826. int := expression(SyntaxTree.EnumerationValue).value;
  1827. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  1828. int := Global.ConvertSigned(int,system.SizeOf(type));
  1829. result := SyntaxTree.NewIntegerValue(position,int);
  1830. result.SetType(type);
  1831. ELSE
  1832. result := expression;
  1833. END;
  1834. (* nothing to be done *)
  1835. ELSE
  1836. Error(position, "expression cannot be converted");
  1837. IF VerboseErrorMessage THEN
  1838. Printout.Info("expression",expression);
  1839. Printout.Info("type",type);
  1840. END;
  1841. result := SyntaxTree.invalidExpression;
  1842. END;
  1843. RETURN result
  1844. END ConvertValue;
  1845. (**
  1846. return a conversion of an expression to a given type
  1847. - if expression is already of same type then return expression
  1848. - if incompatible conversion then report error and return invalidExpression
  1849. **)
  1850. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  1851. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  1852. BEGIN
  1853. type := type.resolved;
  1854. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  1855. result := expression;
  1856. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  1857. ELSIF expression = NIL THEN (* NIL expression *)
  1858. ELSIF expression.type = NIL THEN
  1859. Error(position, "expression of type NIL cannot be converted");
  1860. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  1861. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  1862. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  1863. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  1864. ELSIF expression.resolved # NIL THEN (* value *)
  1865. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  1866. IF value IS SyntaxTree.Value THEN
  1867. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  1868. result.SetResolved(value(SyntaxTree.Value));
  1869. result.SetType(value.type);
  1870. ELSE
  1871. result := value
  1872. END;
  1873. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  1874. expressionList := SyntaxTree.NewExpressionList();
  1875. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  1876. typeDeclaration.SetDeclaredType(type);
  1877. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  1878. typeSymbol.SetType(typeDeclaration.type);
  1879. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  1880. expressionList.AddExpression(expression);
  1881. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  1882. result.SetType(type);
  1883. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  1884. IF inConversion>5 THEN
  1885. Error(expression.position,"recursive Conversion");
  1886. IF VerboseErrorMessage THEN
  1887. Printout.Info("expression",expression);
  1888. Printout.Info("type",type);
  1889. END;
  1890. END;
  1891. INC(inConversion);
  1892. IF inConversion < 10 THEN
  1893. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  1894. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  1895. result := expression
  1896. ELSE
  1897. result := MathArrayConversion(position, expression,type);
  1898. END;
  1899. ELSE
  1900. Error(expression.position,"cannot convert non array type to array type")
  1901. END;
  1902. END;
  1903. DEC(inConversion);
  1904. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  1905. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  1906. result := expression; result.SetType(type);
  1907. ELSIF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  1908. OR ~(type IS SyntaxTree.ArrayType) THEN
  1909. Error(expression.position,"cannot convert array type to non-array type")
  1910. END;
  1911. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  1912. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  1913. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  1914. (*skip, no conversion*)
  1915. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  1916. (* skip, no conversion *)
  1917. ELSE
  1918. ASSERT(~(type IS SyntaxTree.RangeType));
  1919. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  1920. ASSERT(type # NIL);
  1921. END;
  1922. RETURN result
  1923. END NewConversion;
  1924. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  1925. BEGIN
  1926. IF CompatibleTo(system,expression.type, type) THEN
  1927. RETURN NewConversion(position, expression, type, NIL);
  1928. ELSE
  1929. Error(expression.position, "incompatible expression");
  1930. RETURN SyntaxTree.invalidExpression
  1931. END;
  1932. END CompatibleConversion;
  1933. (**
  1934. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  1935. **)
  1936. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  1937. VAR leftType,rightType: SyntaxTree.Type;
  1938. BEGIN
  1939. IF left.type = NIL THEN Error(left.position,"no type")
  1940. ELSIF right.type= NIL THEN Error(right.position,"no type")
  1941. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  1942. (* error already handled *)
  1943. ELSE
  1944. leftType := left.type.resolved; rightType := right.type.resolved;
  1945. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  1946. right := NewConversion(right.position, right, leftType, NIL);
  1947. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  1948. left := NewConversion(left.position,left,rightType,NIL);
  1949. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  1950. right := NewConversion(right.position, right, leftType, NIL);
  1951. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  1952. left := NewConversion(left.position,left,rightType,NIL);
  1953. ELSIF CompatibleTo(system,leftType,rightType) THEN
  1954. left := NewConversion(left.position,left,right.type.resolved,NIL);
  1955. ELSIF CompatibleTo(system,rightType,leftType) THEN
  1956. right := NewConversion(right.position,right,left.type.resolved,NIL);
  1957. ELSIF
  1958. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  1959. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  1960. (* must be the case LONGREAL / COMPLEX ) *)
  1961. left := NewConversion(left.position, left, Global.Complex128, NIL);
  1962. right := NewConversion(right.position, right, Global.Complex128, NIL);
  1963. ELSE
  1964. Error(left.position,"incompatible operands");
  1965. END;
  1966. END;
  1967. END ConvertOperands;
  1968. (** find and return best operator matching to parameter list (nil, if none)
  1969. - search current module scope and all (directly or indirectly) imported modules for matching operator
  1970. - take operator with smalles distance, where signature distance is computed in procedure Distance
  1971. **)
  1972. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  1973. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  1974. identifier: SyntaxTree.Identifier;
  1975. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  1976. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  1977. BEGIN
  1978. operator := scope.firstOperator;
  1979. WHILE(operator # NIL) DO
  1980. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  1981. procedureType := operator.type(SyntaxTree.ProcedureType);
  1982. distance := Distance(system, procedureType,actualParameters);
  1983. IF (distance < Infinity) THEN
  1984. IF returnType # NIL THEN
  1985. IF procedureType.returnType = NIL THEN
  1986. distance := Infinity
  1987. ELSE
  1988. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  1989. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  1990. END;
  1991. END;
  1992. END;
  1993. (*
  1994. IF distance < Infinity THEN
  1995. TRACE(distance, operator);
  1996. Printout.Info("potential operator",operator);
  1997. ELSE
  1998. Printout.Info("operator not possible",operator);
  1999. END;
  2000. *)
  2001. IF distance < bestDistance THEN
  2002. bestDistance := distance;
  2003. bestOperator := operator;
  2004. END;
  2005. END;
  2006. operator := operator.nextOperator;
  2007. END;
  2008. (*
  2009. Printout.Info("taken operator",bestOperator);
  2010. *)
  2011. END FindInScope;
  2012. BEGIN
  2013. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2014. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2015. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2016. import := currentScope.ownerModule.moduleScope.firstImport;
  2017. WHILE (bestDistance > 0) & (import # NIL) DO
  2018. IF import.module # NIL THEN
  2019. identifier := Global.GetIdentifier(operator,import.module.case);
  2020. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2021. END;
  2022. import := import.nextImport;
  2023. END;
  2024. RETURN bestOperator
  2025. END FindOperator;
  2026. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2027. BEGIN
  2028. currentScope := scope;
  2029. END SetCurrentScope;
  2030. (**
  2031. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2032. - handle LEN and DIM operator for array-structured object types
  2033. - find operator, if found then
  2034. - if in other module then add import designator
  2035. - create symbol designator for operator
  2036. - if error then return invalidExpression, if no operator then return NIL
  2037. **)
  2038. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2039. VAR
  2040. operator: SyntaxTree.Operator;
  2041. import: SyntaxTree.Import;
  2042. expression, result: SyntaxTree.Expression;
  2043. designator: SyntaxTree.Designator;
  2044. actualParameters: SyntaxTree.ExpressionList;
  2045. castReturnType : SyntaxTree.MathArrayType;
  2046. BEGIN
  2047. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2048. result := SyntaxTree.invalidExpression
  2049. ELSIF leftExpression = NIL THEN
  2050. result := NIL
  2051. ELSE
  2052. IF ~complexNumbersImported THEN
  2053. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2054. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2055. THEN
  2056. (* operators on complex numbers *)
  2057. ImportModule(Global.ComplexNumbersName,position);
  2058. complexNumbersImported := TRUE;
  2059. END;
  2060. END;
  2061. (* import OCArrayBase if needed *)
  2062. IF ~arrayBaseImported THEN
  2063. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2064. IF op = Global.Dim THEN
  2065. (* not existing in OCArrayBase *)
  2066. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2067. (* not existing in OCArrayBase *)
  2068. ELSE
  2069. ImportModule(Global.ArrayBaseName,position);
  2070. arrayBaseImported := TRUE;
  2071. END
  2072. END;
  2073. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2074. (* LEN(RANGE) *)
  2075. ImportModule(Global.ArrayBaseName,position);
  2076. arrayBaseImported := TRUE;
  2077. END;
  2078. END;
  2079. actualParameters := SyntaxTree.NewExpressionList();
  2080. actualParameters.AddExpression(leftExpression);
  2081. IF rightExpression # NIL THEN
  2082. actualParameters.AddExpression(rightExpression)
  2083. END;
  2084. operator := FindOperator(system,op,actualParameters,resultType);
  2085. IF operator # NIL THEN
  2086. designator := NIL;
  2087. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2088. import := currentScope.ownerModule.moduleScope.firstImport;
  2089. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2090. import := import.nextImport;
  2091. END;
  2092. expression := NewSymbolDesignator(position,NIL,import);
  2093. designator := expression(SyntaxTree.Designator);
  2094. END;
  2095. expression := NewSymbolDesignator(position,designator,operator);
  2096. designator := expression(SyntaxTree.Designator);
  2097. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2098. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2099. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2100. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2101. result.SetType(castReturnType);
  2102. END;
  2103. ELSE
  2104. result := NIL;
  2105. END;
  2106. END;
  2107. RETURN result
  2108. END NewOperatorCall;
  2109. (** check and resolve binary expression **)
  2110. PROCEDURE ResolveBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression): SyntaxTree.Expression;
  2111. VAR left,right,result: SyntaxTree.Expression;
  2112. leftType, rightType: SyntaxTree.Type;
  2113. 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;
  2114. cl,cr: CHAR;
  2115. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2116. type: SyntaxTree.Type;
  2117. value: SyntaxTree.Value;
  2118. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: Basic.Integer;
  2119. integerConstantFolding: BOOLEAN;
  2120. list: SyntaxTree.ExpressionList;
  2121. PROCEDURE NewBool(v: BOOLEAN);
  2122. BEGIN
  2123. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2124. value.SetType(system.booleanType);
  2125. result.SetResolved(value);
  2126. type := system.booleanType
  2127. END NewBool;
  2128. PROCEDURE NewSet(v: Basic.Set);
  2129. BEGIN
  2130. value := Global.NewSetValue(system,binaryExpression.position,v);
  2131. result.SetResolved(value);
  2132. type := value.type;
  2133. END NewSet;
  2134. PROCEDURE NewInteger(v: Basic.Integer; t: SyntaxTree.Type);
  2135. BEGIN
  2136. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2137. (* type cast to "larger" type only if the value is still in the range *)
  2138. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2139. value.SetType(t);
  2140. END;
  2141. result.SetResolved(value);
  2142. type := value.type;
  2143. END NewInteger;
  2144. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2145. BEGIN
  2146. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2147. value.SetType(t);
  2148. result.SetResolved(value);
  2149. type := t;
  2150. END NewReal;
  2151. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2152. BEGIN
  2153. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2154. value.SetType(t);
  2155. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2156. result.SetResolved(value);
  2157. type := t;
  2158. END NewComplex;
  2159. BEGIN
  2160. type := SyntaxTree.invalidType;
  2161. left := ResolveExpression(binaryExpression.left);
  2162. right := ResolveExpression(binaryExpression.right);
  2163. binaryExpression.SetLeft(left);
  2164. binaryExpression.SetRight(right);
  2165. result := binaryExpression;
  2166. operator := binaryExpression.operator;
  2167. IF ~system.operatorDefined[operator] THEN
  2168. Error(left.position,"Operator Not Defined");
  2169. RETURN SyntaxTree.invalidExpression;
  2170. END;
  2171. IF left.type = NIL THEN
  2172. Error(left.position,"Expression has no result type");
  2173. RETURN SyntaxTree.invalidExpression;
  2174. END;
  2175. IF right.type = NIL THEN
  2176. Error(right.position,"Expression has no result type");
  2177. RETURN SyntaxTree.invalidExpression;
  2178. END;
  2179. leftType := left.type.resolved; rightType := right.type.resolved;
  2180. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2181. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2182. END;
  2183. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2184. list := SyntaxTree.NewExpressionList();
  2185. list.AddExpression(right);
  2186. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2187. END;
  2188. IF operatorCall # NIL THEN
  2189. result := operatorCall;
  2190. type := operatorCall.type;
  2191. (* admissible operators:
  2192. Times, Plus, Minus numeric numeric numeric
  2193. set set set
  2194. Slash numeric numeric real /complex
  2195. set set set
  2196. Div , Mod integer integer integer
  2197. And, Or bool bool bool
  2198. Equal, Unequal basic basic bool
  2199. pointer pointer bool
  2200. object object bool
  2201. record record bool
  2202. string string bool
  2203. enumerator enumerator bool
  2204. Less, LessEqual,
  2205. Greater, GreaterEqual integer/real integer/real bool
  2206. enumerator enumerator bool
  2207. In integer set bool
  2208. Is pointer type bool
  2209. object type bool
  2210. record type bool
  2211. Upto: special abbreviation for a..b
  2212. *)
  2213. ELSIF (left.type = NIL) THEN
  2214. Error(left.position,"type (left operand) = NIL in binary expression");
  2215. D.Str("nil type in "); D.Type(left); D.Ln;
  2216. result := SyntaxTree.invalidExpression;
  2217. ELSIF (right.type = NIL) THEN
  2218. Error(right.position,"type (right operand) = NIL in binary expression");
  2219. result := SyntaxTree.invalidExpression;
  2220. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2221. result := SyntaxTree.invalidExpression;
  2222. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2223. HALT(100);
  2224. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2225. type := system.booleanType;
  2226. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2227. Error(right.position,"is not a type ");
  2228. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2229. Error(binaryExpression.position,"is not a type extension of ");
  2230. IF VerboseErrorMessage THEN
  2231. Printout.Info("left",left);
  2232. Printout.Info("right",right);
  2233. END;
  2234. ELSIF IsUnsafePointer(left.type) THEN
  2235. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2236. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2237. NewBool(TRUE)
  2238. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2239. NewBool(TRUE);
  2240. ELSIF IsUnextensibleRecord(left) THEN
  2241. NewBool(FALSE)
  2242. END
  2243. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2244. Error(right.position,"must not be a type");
  2245. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2246. Error(left.position,"must not be a type");
  2247. ELSIF operator = Scanner.In THEN (* left IN right *)
  2248. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2249. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2250. NewBool(il IN sr);
  2251. ELSE
  2252. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2253. left := NewConversion(left.position, left, system.longintType,NIL);
  2254. binaryExpression.SetLeft(left)
  2255. END;
  2256. type := system.booleanType;
  2257. END
  2258. ELSE
  2259. Error(binaryExpression.position, "incompatible operands");
  2260. END
  2261. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2262. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2263. Error(binaryExpression.position,"incompatible operands");
  2264. END;
  2265. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2266. ELSE Error(binaryExpression.position,"operator not defined 1")
  2267. END
  2268. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2269. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2270. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2271. Error(binaryExpression.position,"incompatible operands");
  2272. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2273. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2274. left := NewConversion(left.position, left, system.addressType, NIL);
  2275. right := NewConversion(right.position, right, system.addressType, NIL);
  2276. binaryExpression.SetLeft(left);
  2277. binaryExpression.SetRight(right);
  2278. type := system.addressType;
  2279. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2280. ConvertOperands(left, right);
  2281. binaryExpression.SetLeft(left);
  2282. binaryExpression.SetRight(right);
  2283. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2284. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2285. END;
  2286. type := system.booleanType;
  2287. ELSE
  2288. Error(binaryExpression.position,"operator not defined 3");
  2289. END
  2290. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2291. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2292. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2293. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2294. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2295. CASE operator OF
  2296. |Scanner.Equal: NewBool(strl^=strr^);
  2297. |Scanner.Unequal:NewBool(strl^#strr^);
  2298. |Scanner.Less: NewBool(strl^<strr^);
  2299. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2300. |Scanner.Greater: NewBool(strl^>strr^);
  2301. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2302. ELSE
  2303. Error(binaryExpression.position,"operator not defined 4");
  2304. END;
  2305. END;
  2306. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2307. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2308. type := system.booleanType
  2309. ELSE
  2310. Error(binaryExpression.position,"operator not defined 5");
  2311. END;
  2312. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2313. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2314. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2315. type := system.booleanType;
  2316. ELSE
  2317. Error(binaryExpression.position,"operator not defined 6");
  2318. END
  2319. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2320. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2321. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2322. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2323. type := system.booleanType
  2324. ELSE
  2325. Error(binaryExpression.position,"operator not defined for enumerators");
  2326. END;
  2327. ELSE
  2328. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2329. END;
  2330. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2331. type := system.booleanType;
  2332. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2333. type := system.booleanType;
  2334. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2335. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2336. THEN
  2337. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2338. IF (leftType # rightType) THEN
  2339. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2340. ConvertOperands(left,right); (* operands must be of the same type here *)
  2341. END;
  2342. binaryExpression.SetLeft(left);
  2343. binaryExpression.SetRight(right);
  2344. leftType := left.type.resolved;
  2345. rightType := right.type.resolved;
  2346. END;
  2347. type := leftType;
  2348. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2349. Error(binaryExpression.position,"conversion failed ?");
  2350. IF VerboseErrorMessage THEN
  2351. Printout.Info("left",left);
  2352. Printout.Info("right",right);
  2353. END;
  2354. ELSIF IsIntegerType(leftType) THEN
  2355. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2356. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2357. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2358. Error(binaryExpression.position,"division by zero");
  2359. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2360. Error(binaryExpression.position,"integer division by negative number");
  2361. END;
  2362. END;
  2363. (* constant folding *)
  2364. (* bootstrap64
  2365. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2366. CASE operator OF
  2367. |Scanner.Plus: NewInteger(il+ir,left.type);
  2368. |Scanner.Minus: NewInteger(il-ir,left.type);
  2369. |Scanner.Times: NewInteger(il*ir,left.type);
  2370. |Scanner.Slash:
  2371. IF ir # 0 THEN
  2372. NewReal(il/ir, system.realType);
  2373. END;
  2374. |Scanner.Mod:
  2375. IF ir > 0 THEN
  2376. NewInteger(il MOD ir,left.type);
  2377. END;
  2378. |Scanner.Div:
  2379. IF ir > 0 THEN
  2380. NewInteger(il DIV ir,left.type);
  2381. END;
  2382. |Scanner.Equal: NewBool(il=ir);
  2383. |Scanner.Unequal:NewBool(il#ir);
  2384. |Scanner.Less: NewBool(il<ir);
  2385. |Scanner.LessEqual: NewBool(il<=ir);
  2386. |Scanner.Greater: NewBool(il>ir);
  2387. |Scanner.GreaterEqual: NewBool(il>=ir);
  2388. ELSE Error(binaryExpression.position,"operator not defined 7");
  2389. END;
  2390. ELS*)
  2391. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2392. hl := left.resolved(SyntaxTree.IntegerValue).value;
  2393. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2394. CASE operator OF
  2395. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2396. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2397. |Scanner.Times: NewInteger(hl*hr,left.type);
  2398. |Scanner.Slash:
  2399. IF hr = 0 THEN
  2400. Error(binaryExpression.position,"division by zero");
  2401. ELSE
  2402. IF type.sizeInBits = 64 THEN
  2403. NewReal(hl/hr,system.longrealType);
  2404. ELSE
  2405. NewReal(hl/hr,system.realType)
  2406. END
  2407. END;
  2408. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2409. |Scanner.Mod:
  2410. IF hr = 0 THEN
  2411. Error(binaryExpression.position,"division by zero");
  2412. ELSE
  2413. NewInteger(hl MOD hr, left.type);
  2414. (* bootstrap64
  2415. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2416. *)
  2417. END;
  2418. |Scanner.Div:
  2419. IF hr = 0 THEN
  2420. Error(binaryExpression.position,"division by zero");
  2421. ELSE
  2422. NewInteger(hl DIV hr, left.type);
  2423. (* bootstrap64
  2424. NewInteger(Machine.DivH(hl,hr),left.type);
  2425. *)
  2426. END;
  2427. (* *)
  2428. |Scanner.Equal: NewBool(hl=hr);
  2429. |Scanner.Unequal: NewBool(hl#hr);
  2430. |Scanner.Less: NewBool(hl<hr);
  2431. |Scanner.LessEqual: NewBool(hl<=hr);
  2432. |Scanner.Greater: NewBool(hl>hr);
  2433. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2434. ELSE Error(binaryExpression.position,"operator not defined 8");
  2435. END;
  2436. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2437. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2438. type := left.type
  2439. ELSIF (operator = Scanner.Slash) THEN
  2440. left := NewConversion(left.position,left,system.realType,NIL);
  2441. right := NewConversion(right.position,right,system.realType,NIL);
  2442. binaryExpression.SetLeft(left);
  2443. binaryExpression.SetRight(right);
  2444. type := system.realType
  2445. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2446. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2447. type := system.booleanType
  2448. ELSE
  2449. Error(binaryExpression.position,"operator not defined 9");
  2450. END;
  2451. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2452. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2453. CASE operator OF
  2454. |Scanner.Plus: NewReal(rl+rr,leftType);
  2455. |Scanner.Minus: NewReal(rl-rr,leftType);
  2456. |Scanner.Times:NewReal(rl*rr,leftType);
  2457. |Scanner.Slash:
  2458. IF rr = 0 THEN
  2459. Error(binaryExpression.position,"division by zero");
  2460. ELSE
  2461. NewReal(rl/rr,leftType);
  2462. END
  2463. |Scanner.Equal: NewBool(rl=rr);
  2464. |Scanner.Unequal: NewBool(rl#rr);
  2465. |Scanner.Less: NewBool(rl<rr);
  2466. |Scanner.LessEqual: NewBool(rl<=rr);
  2467. |Scanner.Greater: NewBool(rl>rr);
  2468. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2469. ELSE Error(binaryExpression.position,"operator not defined 10");
  2470. END;
  2471. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2472. type := left.type
  2473. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2474. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2475. type := system.booleanType
  2476. ELSE
  2477. Error(binaryExpression.position,"operator not defined 11");
  2478. IF VerboseErrorMessage THEN
  2479. Printout.Info("left",left);
  2480. Printout.Info("right",right);
  2481. END;
  2482. END;
  2483. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2484. CASE operator OF
  2485. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2486. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2487. ELSE
  2488. Error(binaryExpression.position,"operator not defined");
  2489. IF VerboseErrorMessage THEN
  2490. Printout.Info("left", left);
  2491. Printout.Info("right", right)
  2492. END;
  2493. END;
  2494. IF ~error THEN
  2495. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2496. Error(binaryExpression.position,"division by zero")
  2497. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2498. (* do constant folding *)
  2499. CASE operator OF
  2500. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2501. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2502. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2503. |Scanner.Slash:
  2504. divisor := c * c + d * d;
  2505. ASSERT(divisor # 0);
  2506. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2507. |Scanner.Equal: NewBool((a = c) & (b = d))
  2508. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2509. END
  2510. END
  2511. END
  2512. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2513. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2514. CASE operator OF
  2515. |Scanner.And: NewBool(bl & br);
  2516. |Scanner.Or: NewBool(bl OR br);
  2517. |Scanner.Equal: NewBool(bl = br);
  2518. |Scanner.Unequal: NewBool(bl # br);
  2519. ELSE Error(binaryExpression.position,"operator not defined 12");
  2520. END;
  2521. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2522. type := system.booleanType
  2523. ELSE
  2524. Error(binaryExpression.position,"operator not defined 13");
  2525. END;
  2526. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2527. (* constant folding *)
  2528. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2529. IF operator = Scanner.Equal THEN
  2530. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2531. ELSIF operator = Scanner.Unequal THEN
  2532. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2533. END;
  2534. END;
  2535. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2536. type := system.booleanType;
  2537. ELSE
  2538. Error(binaryExpression.position, "operator not defined");
  2539. END;
  2540. ELSIF (leftType IS SyntaxTree.SetType) THEN
  2541. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  2542. CASE operator OF
  2543. |Scanner.Plus: NewSet(sl + sr);
  2544. |Scanner.Minus: NewSet(sl - sr);
  2545. |Scanner.Times: NewSet(sl * sr);
  2546. |Scanner.Slash: NewSet(sl / sr);
  2547. |Scanner.Equal: NewBool(sl=sr);
  2548. |Scanner.Unequal: NewBool(sl#sr);
  2549. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  2550. |Scanner.LessEqual: NewBool(sl*sr = sl);
  2551. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  2552. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  2553. ELSE Error(binaryExpression.position,"operator not defined 14");
  2554. END;
  2555. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  2556. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2557. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  2558. THEN
  2559. type := system.booleanType
  2560. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2561. type := left.type
  2562. ELSE
  2563. Error(binaryExpression.position,"operator not defined 15");
  2564. END;
  2565. ELSIF IsCharacterType(left.type) THEN
  2566. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  2567. CASE operator OF
  2568. |Scanner.Equal: NewBool(cl=cr);
  2569. |Scanner.Unequal: NewBool(cl#cr);
  2570. |Scanner.Less: NewBool(cl<cr);
  2571. |Scanner.LessEqual: NewBool(cl<=cr);
  2572. |Scanner.Greater: NewBool(cl>cr);
  2573. |Scanner.GreaterEqual: NewBool(cl>=cr);
  2574. ELSE Error(binaryExpression.position,"operator not defined 16");
  2575. END;
  2576. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2577. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2578. type := system.booleanType
  2579. ELSE
  2580. Error(binaryExpression.position,"operator not defined 17");
  2581. END;
  2582. ELSE
  2583. Error(binaryExpression.position,"operator not defined 18");
  2584. END;
  2585. ELSE
  2586. Error(binaryExpression.position,"operator not defined 19");
  2587. END;
  2588. IF type = SyntaxTree.invalidType THEN
  2589. result := SyntaxTree.invalidExpression
  2590. ELSE
  2591. result.SetType(type)
  2592. END;
  2593. RETURN result
  2594. END ResolveBinaryExpression;
  2595. (** resolve a range expression of the from <<first .. last BY step>>
  2596. - depending on the context different things are checked:
  2597. ArrayIndex:
  2598. - components must be integers
  2599. - replace missing lower bound with 0
  2600. - replace missing upper bound with MAX(LONGINT)
  2601. - replace missing step size with 1
  2602. SetElement:
  2603. - components must be integers
  2604. - replace missing lower bound with 0
  2605. - replace missing upper bound with MAX(SET)
  2606. - must not have step size
  2607. CaseGuard:
  2608. - components must be constant
  2609. - components must be integers or characters
  2610. - must have lower and upper bound present
  2611. - components are made compatible
  2612. - must not have step size
  2613. - if error: return invalidExpression
  2614. **)
  2615. PROCEDURE ResolveRangeExpression(x: SyntaxTree.RangeExpression): SyntaxTree.Expression;
  2616. VAR
  2617. hasError: BOOLEAN;
  2618. first, last, step: SyntaxTree.Expression;
  2619. BEGIN
  2620. hasError := FALSE;
  2621. first := x.first;
  2622. last := x.last;
  2623. step := x.step;
  2624. (* check lower bound *)
  2625. IF x.context = SyntaxTree.CaseGuard THEN
  2626. IF first = NIL THEN
  2627. Error(x.position, "missing lower bound");
  2628. hasError := TRUE
  2629. ELSE
  2630. first := ResolveExpression(first);
  2631. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  2632. Error(first.position, "lower bound not integer or character");
  2633. hasError := TRUE
  2634. ELSE
  2635. IF first IS SyntaxTree.StringValue THEN
  2636. (* add conversion from string to character *)
  2637. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  2638. END
  2639. END;
  2640. (* check if expression is constant *)
  2641. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  2642. (* error already reported *)
  2643. hasError := TRUE
  2644. END
  2645. END
  2646. ELSE (* ArrayIndex, SetElement *)
  2647. IF first = NIL THEN
  2648. first := SyntaxTree.NewIntegerValue(x.position, 0);
  2649. END;
  2650. first := ResolveExpression(first);
  2651. IF IsIntegerType(first.type.resolved) THEN
  2652. first := NewConversion(first.position, first, system.lenType, NIL)
  2653. ELSE
  2654. Error(first.position, "lower bound not integer");
  2655. hasError := TRUE
  2656. END
  2657. END;
  2658. (* check upper bound *)
  2659. IF x.context = SyntaxTree.CaseGuard THEN
  2660. IF last = NIL THEN
  2661. Error(x.position, "missing upper bound");
  2662. hasError := TRUE
  2663. ELSE
  2664. last := ResolveExpression(last);
  2665. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  2666. Error(last.position, "lower bound not integer or character");
  2667. hasError := TRUE
  2668. ELSE
  2669. IF last IS SyntaxTree.StringValue THEN
  2670. (* add conversion from string to character *)
  2671. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  2672. END
  2673. END;
  2674. (* check if expression is constant *)
  2675. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  2676. (* error already reported *)
  2677. hasError := TRUE
  2678. ELSE
  2679. (* try to make lower and upper bound compatible *)
  2680. ConvertOperands(first, last);
  2681. IF first.type.resolved # last.type.resolved THEN
  2682. Error(x.position, "lower and upper bounds incompatible");
  2683. hasError := TRUE
  2684. END
  2685. END
  2686. END
  2687. ELSE (* ArrayIndex, SetElement *)
  2688. IF last = NIL THEN
  2689. IF x.context = SyntaxTree.ArrayIndex THEN
  2690. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  2691. ELSE
  2692. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  2693. END
  2694. END;
  2695. last := ResolveExpression(last);
  2696. IF IsIntegerType(last.type.resolved) THEN
  2697. last := NewConversion(last.position, last, system.lenType, NIL)
  2698. ELSE
  2699. Error(last.position, "upper bound not integer");
  2700. hasError := TRUE
  2701. END
  2702. END;
  2703. (* check step size *)
  2704. IF x.context = SyntaxTree.ArrayIndex THEN
  2705. IF step = NIL THEN
  2706. step := SyntaxTree.NewIntegerValue(x.position, 1)
  2707. END;
  2708. step := ResolveExpression(step);
  2709. IF IsIntegerType(step.type.resolved) THEN
  2710. step := NewConversion(step.position, step, system.lenType, NIL)
  2711. ELSE
  2712. Error(step.position, "step size not integer");
  2713. hasError := TRUE
  2714. END
  2715. ELSE (* SetElement, CaseGuard *)
  2716. IF step # NIL THEN
  2717. Error(last.position, "step size not allowed in this context");
  2718. hasError := TRUE
  2719. END
  2720. END;
  2721. IF hasError THEN
  2722. RETURN SyntaxTree.invalidExpression
  2723. ELSE
  2724. x.SetFirst(first);
  2725. x.SetLast(last);
  2726. x.SetStep(step);
  2727. x.SetType(system.rangeType);
  2728. x.SetAssignable(FALSE); (* range expressions may never be assigned to *)
  2729. RETURN x;
  2730. END
  2731. END ResolveRangeExpression;
  2732. (** resolve the expression d and return result as designator
  2733. - resolve expression
  2734. - if expression is a designator then return designator else error message and return invalidDesignator
  2735. **)
  2736. PROCEDURE ResolveDesignator(d: SyntaxTree.Expression): SyntaxTree.Designator;
  2737. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  2738. BEGIN
  2739. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  2740. resolved := ResolveExpression(d);
  2741. IF resolved = SyntaxTree.invalidExpression THEN
  2742. (* error should already have been reported *)
  2743. result := SyntaxTree.invalidDesignator;
  2744. ELSIF resolved IS SyntaxTree.Designator THEN
  2745. result := resolved(SyntaxTree.Designator);
  2746. ELSE
  2747. Error(d.position,"is no designator ! ");
  2748. result := SyntaxTree.invalidDesignator;
  2749. END;
  2750. (* result.type might be nil. *)
  2751. RETURN result
  2752. END ResolveDesignator;
  2753. (**
  2754. self designator generated in this module
  2755. nothing to be resolved
  2756. **)
  2757. PROCEDURE ResolveSelfDesignator(x: SyntaxTree.SelfDesignator): SyntaxTree.Expression;
  2758. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  2759. BEGIN
  2760. (* check if in record scope *)
  2761. scope := currentScope;
  2762. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).selfParameter # NIL) THEN
  2763. RETURN NewSymbolDesignator(x.position, NIL, scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).selfParameter);
  2764. END;
  2765. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  2766. scope := scope.outerScope;
  2767. END;
  2768. IF scope = NIL THEN (* in module scope *)
  2769. x.SetType(system.anyType);
  2770. ELSIF scope IS SyntaxTree.CellScope THEN
  2771. cell := scope(SyntaxTree.CellScope).ownerCell;
  2772. x.SetType(cell);
  2773. ELSE (* in record scope *)
  2774. record := scope(SyntaxTree.RecordScope).ownerRecord;
  2775. IF (record # NIL) & (record.pointerType # NIL) THEN
  2776. type := ResolveType(record.pointerType);
  2777. x.SetType(type);
  2778. ELSE
  2779. x.SetType(record);
  2780. x.SetAssignable(TRUE); (* var parameter *)
  2781. END;
  2782. END;
  2783. RETURN x;
  2784. END ResolveSelfDesignator;
  2785. PROCEDURE ResolveResultDesignator(x: SyntaxTree.ResultDesignator): SyntaxTree.Expression;
  2786. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  2787. BEGIN
  2788. scope := currentScope;
  2789. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  2790. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  2791. procedureType := procedure.type(SyntaxTree.ProcedureType);
  2792. returnType := procedureType.returnType;
  2793. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  2794. THEN
  2795. x.SetType(returnType);
  2796. ELSE
  2797. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  2798. x.SetType(SyntaxTree.invalidType);
  2799. END;
  2800. ELSE
  2801. Error(x.position,"forbidden access to result designator");
  2802. x.SetType(SyntaxTree.invalidType);
  2803. END;
  2804. x.SetAssignable(TRUE);
  2805. RETURN x;
  2806. END ResolveResultDesignator;
  2807. (**
  2808. return symbol designator as an expression
  2809. - if symbol is a constant then return the constant value expression
  2810. - else
  2811. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  2812. - if symbol is a guarded variable then return a TypeGuardDesignator
  2813. - else return a symbol designator
  2814. **)
  2815. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  2816. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  2817. guardType: SyntaxTree.Type;
  2818. BEGIN
  2819. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  2820. result := SyntaxTree.invalidExpression;
  2821. ASSERT(symbol # NIL);
  2822. (*
  2823. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  2824. IF symbol IS SyntaxTree.TypeDeclaration THEN
  2825. Error(position, "type not allowed here");
  2826. ELS *)
  2827. (* not needed any more as values are stored in the expression
  2828. IF symbol IS SyntaxTree.Constant THEN
  2829. result := symbol(SyntaxTree.Constant).value
  2830. IF symbol(SyntaxTree.Constant).value # NIL THEN
  2831. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  2832. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  2833. ELSE
  2834. result := symbol(SyntaxTree.Constant).value
  2835. END;
  2836. ELSE
  2837. *)
  2838. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  2839. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  2840. THEN
  2841. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  2842. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  2843. left := NewDereferenceDesignator(position,left);
  2844. left.SetHidden(TRUE);
  2845. END;
  2846. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  2847. scope := currentScope;
  2848. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  2849. scope := scope.outerScope;
  2850. END;
  2851. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  2852. Error(position, "forbidden access to symbol in parent procedure scope");
  2853. END;
  2854. END;
  2855. 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);
  2856. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  2857. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  2858. ELSE
  2859. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  2860. END;
  2861. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  2862. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  2863. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  2864. result.SetType(symbol.type);
  2865. result.SetAssignable(assignable);
  2866. symbol.MarkUsed;
  2867. IF symbol IS SyntaxTree.Constant THEN
  2868. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  2869. END;
  2870. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  2871. variableAccessed := TRUE
  2872. END;
  2873. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  2874. IF GetGuard(symbol,guardType) THEN
  2875. IF (symbol.type.resolved IS SyntaxTree.RecordType) OR ~assignable THEN (* type guard is tested and type cannot be changed *)
  2876. result.SetType(guardType);
  2877. ELSE
  2878. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  2879. END;
  2880. END;
  2881. END;
  2882. ASSERT(result.type # NIL);
  2883. RETURN result
  2884. END NewSymbolDesignator;
  2885. (** check and resolve an identifier designator "identifier"
  2886. - if identifier = self then return SelfDesignator
  2887. - else find symbol in current scope
  2888. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  2889. **)
  2890. PROCEDURE ResolveIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator): SyntaxTree.Expression;
  2891. VAR symbol: SyntaxTree.Symbol;
  2892. BEGIN
  2893. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  2894. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  2895. IF symbol # NIL THEN
  2896. ResolveSymbol(symbol);
  2897. ASSERT(symbol.type # NIL);
  2898. RETURN NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  2899. ELSE
  2900. Error(identifierDesignator.position,"Undeclared Identifier");
  2901. IF VerboseErrorMessage THEN
  2902. Printout.Info("undeclared identifier designator",identifierDesignator);
  2903. END;
  2904. RETURN SyntaxTree.invalidDesignator;
  2905. END;
  2906. END ResolveIdentifierDesignator;
  2907. (** check and resolve a selector designator of the form left.designator
  2908. - if left is a pointer type then do auto dereferenciation
  2909. - left denotes a search scope:
  2910. - if left type is import type then set search scope to respective module
  2911. - if left type is enumeration type then set search scope to respective enumeration scope
  2912. - elsif left type is record type then set search scope to record scope
  2913. - search symbol in computed scope
  2914. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  2915. **)
  2916. PROCEDURE ResolveSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator): SyntaxTree.Expression;
  2917. VAR
  2918. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  2919. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  2920. BEGIN
  2921. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  2922. left := ResolveDesignator(selectorDesignator.left);
  2923. result := SyntaxTree.invalidDesignator;
  2924. IF left # NIL THEN
  2925. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  2926. left := NewDereferenceDesignator(selectorDesignator.position,left);
  2927. END;
  2928. scope := NIL;
  2929. IF left.type = NIL THEN
  2930. Error(selectorDesignator.position,"field on nil typed designator");
  2931. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  2932. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  2933. ELSIF left.type.resolved = SyntaxTree.importType THEN
  2934. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  2935. module := symbol(SyntaxTree.Import).module;
  2936. IF module # NIL THEN
  2937. scope := module.moduleScope
  2938. ELSE
  2939. Error(left.position,"module not loaded");
  2940. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  2941. END;
  2942. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  2943. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  2944. ASSERT(scope # NIL)
  2945. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  2946. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  2947. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  2948. IF type IS SyntaxTree.EnumerationType THEN
  2949. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  2950. ELSE
  2951. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  2952. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  2953. END;
  2954. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  2955. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  2956. ELSE
  2957. Error(selectorDesignator.position,"field on non-record type designator");
  2958. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  2959. END;
  2960. symbol := NIL;
  2961. IF scope # NIL THEN
  2962. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  2963. IF symbol # NIL THEN
  2964. ResolveSymbol(symbol);
  2965. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  2966. symbol.MarkUsed
  2967. ELSE
  2968. Error(selectorDesignator.position,"undeclared identifier (selector)");
  2969. IF VerboseErrorMessage THEN
  2970. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  2971. Printout.Info("scope", scope);
  2972. Printout.Info("left", left);
  2973. Printout.Info("undeclared identifier",selectorDesignator);
  2974. Printout.Info("left resolved designator",left);
  2975. END
  2976. END;
  2977. END;
  2978. END;
  2979. RETURN result
  2980. END ResolveSelectorDesignator;
  2981. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  2982. VAR len,idx: Basic.Integer;
  2983. BEGIN
  2984. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  2985. IF idx < 0 THEN
  2986. Error(index.position,"index out of bounds (too small)")
  2987. ELSE
  2988. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  2989. Error(index.position,"index out of bounds (too large)");
  2990. END;
  2991. END;
  2992. END;
  2993. END IndexCheck;
  2994. (*
  2995. - if index designator has not type, use newBaseType as its type
  2996. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  2997. - special rule: if static array of dynamic array occurs, make it all dynamic
  2998. index designator type: new base type: new index designator type:
  2999. NIL z z
  3000. ARRAY [x, y] z ARRAY [x, y] OF z
  3001. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3002. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3003. *)
  3004. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3005. VAR
  3006. mathArrayType: SyntaxTree.MathArrayType;
  3007. makeDynamic: BOOLEAN;
  3008. BEGIN
  3009. IF indexDesignator.type = NIL THEN
  3010. indexDesignator.SetType(newBaseType)
  3011. ELSE
  3012. (* index designator must be a of math array type *)
  3013. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3014. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3015. (* determine if all arrays have to be made dynamic *)
  3016. makeDynamic :=
  3017. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3018. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3019. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3020. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3021. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3022. END;
  3023. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3024. mathArrayType.SetArrayBase(newBaseType)
  3025. END
  3026. END SetIndexBaseType;
  3027. (** check and append index list element to index designator of math array
  3028. - check validity of single index or array range
  3029. - compute new type
  3030. - if range then create new array type (calculate length of resulting array)
  3031. - otherwise take sourceArray.arrayBase as new type
  3032. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3033. **)
  3034. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3035. VAR
  3036. targetArray: SyntaxTree.MathArrayType;
  3037. first, last, step: SyntaxTree.Expression;
  3038. firstValue, lastValue, stepValue, length: Basic.Integer;
  3039. rangeExpression: SyntaxTree.RangeExpression;
  3040. BEGIN
  3041. IF indexListItem.type = SyntaxTree.invalidType THEN
  3042. (* error already handled *)
  3043. indexDesignator.parameters.AddExpression(indexListItem)
  3044. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3045. indexDesignator.HasRange;
  3046. indexDesignator.HasTensorRange;
  3047. indexDesignator.parameters.AddExpression(indexListItem);
  3048. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3049. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3050. IndexCheck(indexListItem, sourceArray.length);
  3051. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3052. indexDesignator.parameters.AddExpression(indexListItem)
  3053. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3054. indexDesignator.HasRange;
  3055. (* if the range is given as an array range expression, check the validity of its components *)
  3056. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3057. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3058. first := rangeExpression.first;
  3059. last := rangeExpression.last;
  3060. step := rangeExpression.step;
  3061. (* perform static checks on range components *)
  3062. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3063. Error(indexListItem.position,"lower bound of array range too small")
  3064. END;
  3065. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3066. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3067. Error(indexListItem.position,"upper bound of array range too large")
  3068. END
  3069. END;
  3070. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3071. Error(indexListItem.position,"invalid step size")
  3072. END;
  3073. (* add conversions to size type *)
  3074. (* TODO: needed? *)
  3075. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3076. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3077. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3078. END;
  3079. IF indexDesignator.hasTensorRange THEN
  3080. (* the index designator's base type is a tensor: leave it as is *)
  3081. ELSE
  3082. (* append a new math array to the index designator's base type *)
  3083. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3084. IF ~error THEN
  3085. (*
  3086. (* optimization: calculate length of target array for static ranges *)
  3087. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3088. IF IsStaticallyOpenRange(rangeExpression) THEN
  3089. (* range is open ('*'): reuse source array length as target array length *)
  3090. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3091. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3092. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3093. IF lastValue = MAX(LONGINT) THEN
  3094. IF IsIntegerValue(sourceArray.length, length) THEN
  3095. lastValue := length - 1;
  3096. isStaticTargetArrayLength := TRUE
  3097. ELSE
  3098. isStaticTargetArrayLength := FALSE
  3099. END
  3100. ELSE
  3101. isStaticTargetArrayLength := TRUE
  3102. END;
  3103. IF isStaticTargetArrayLength THEN
  3104. (* calculate static target array length *)
  3105. IF firstValue > lastValue THEN
  3106. length := 0
  3107. ELSE
  3108. length := 1 + lastValue - firstValue;
  3109. IF length MOD stepValue = 0 THEN
  3110. length := length DIV stepValue
  3111. ELSE
  3112. length := length DIV stepValue + 1
  3113. END
  3114. END;
  3115. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3116. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3117. ASSERT(targetArray.form = SyntaxTree.Static)
  3118. END
  3119. END
  3120. END
  3121. *)
  3122. END;
  3123. SetIndexBaseType(indexDesignator, targetArray)
  3124. END;
  3125. indexDesignator.parameters.AddExpression(indexListItem)
  3126. ELSE
  3127. Error(position,"invalid index list item");
  3128. END;
  3129. END AppendMathIndex;
  3130. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3131. VAR parameters: SyntaxTree.ExpressionList;
  3132. BEGIN
  3133. parameters := index.parameters;
  3134. IF (expression.type = NIL) THEN
  3135. Error(position, "invalid index");
  3136. ELSIF IsIntegerType(expression.type.resolved) THEN
  3137. IF over IS SyntaxTree.ArrayType THEN
  3138. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3139. ELSIF over IS SyntaxTree.StringType THEN
  3140. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3141. END;
  3142. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3143. parameters.AddExpression(expression);
  3144. ELSE
  3145. Error(position, "invalid index");
  3146. END;
  3147. END AppendIndex;
  3148. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3149. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3150. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3151. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3152. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3153. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3154. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3155. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3156. CONST trace = FALSE;
  3157. BEGIN
  3158. IF trace THEN
  3159. FOR i := 0 TO actualParameters.Length()-1 DO
  3160. Printout.Info("par", actualParameters.GetExpression(i));
  3161. END;
  3162. END;
  3163. operator := scope.firstOperator;
  3164. WHILE(operator # NIL) DO
  3165. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3166. procedureType := operator.type(SyntaxTree.ProcedureType);
  3167. distance := Distance(system, procedureType,actualParameters);
  3168. IF trace THEN Printout.Info("check op ",operator) END;
  3169. IF distance < bestDistance THEN
  3170. IF trace THEN Printout.Info("taken op",operator) END;
  3171. bestDistance := distance;
  3172. bestOperator := operator;
  3173. END;
  3174. END;
  3175. operator := operator.nextOperator;
  3176. END;
  3177. END FindInScope;
  3178. BEGIN
  3179. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3180. IF oper = 0 THEN (* index *)
  3181. identifier := SyntaxTree.NewIdentifier("[]");
  3182. ELSE
  3183. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3184. END;
  3185. WHILE (recordType # NIL) DO
  3186. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3187. recordType := recordType.GetBaseRecord();
  3188. END;
  3189. RETURN bestOperator
  3190. END FindOperator;
  3191. BEGIN
  3192. type := left.type.resolved;
  3193. IF type IS SyntaxTree.RecordType THEN
  3194. pointer := FALSE;
  3195. recordType := type(SyntaxTree.RecordType);
  3196. ELSE
  3197. pointer := TRUE;
  3198. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3199. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3200. END;
  3201. actualParameters := SyntaxTree.NewExpressionList();
  3202. IF parameters # NIL THEN
  3203. FOR i := 0 TO parameters.Length()-1 DO
  3204. expression := ResolveExpression(parameters.GetExpression(i));
  3205. actualParameters.AddExpression(expression);
  3206. END;
  3207. END;
  3208. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3209. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3210. IF op # NIL THEN
  3211. designator := left(SyntaxTree.Designator);
  3212. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  3213. expression := NewSymbolDesignator(position, designator , op);
  3214. ASSERT(expression IS SyntaxTree.Designator);
  3215. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3216. (* check if write operator exists, for var parameters *)
  3217. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3218. actualParameters := SyntaxTree.NewExpressionList();
  3219. FOR i := 0 TO parameters.Length()-1 DO
  3220. expression := ResolveExpression(parameters.GetExpression(i));
  3221. actualParameters.AddExpression(expression);
  3222. END;
  3223. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3224. actualParameters.AddExpression(rhs);
  3225. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3226. IF op = NIL THEN rhs := NIL END;
  3227. END;
  3228. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3229. ELSE
  3230. result := NIL;
  3231. END;
  3232. RETURN result;
  3233. END NewObjectOperatorCall;
  3234. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3235. 1. convert bracket designator chains into a single one that contains separators
  3236. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3237. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3238. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3239. - if an array or math array is indexed over, create index designator
  3240. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3241. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3242. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3243. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3244. - if an array-structured object type is indexed over, create procedure call designator
  3245. e.g.: a[x, y] -> a^."[]"(x, y)
  3246. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3247. - a[i, *] = a[i][*]
  3248. - a[*, i] # a[*][i]
  3249. Because:
  3250. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3251. - 'i-th column' = a[*, i]
  3252. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3253. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3254. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3255. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3256. **)
  3257. PROCEDURE ResolveBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator): SyntaxTree.Expression;
  3258. VAR
  3259. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3260. indexDesignator: SyntaxTree.IndexDesignator;
  3261. designator: SyntaxTree.Designator;
  3262. type: SyntaxTree.Type;
  3263. expression: SyntaxTree.Expression;
  3264. i: LONGINT;
  3265. result: SyntaxTree.Expression;
  3266. PROCEDURE FinalizeIndexDesignator;
  3267. BEGIN
  3268. IF indexDesignator # NIL THEN
  3269. (* the end of a tensor has been reached: *)
  3270. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3271. SetIndexBaseType(indexDesignator, type);
  3272. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3273. designator := indexDesignator;
  3274. type := designator.type.resolved;
  3275. indexDesignator := NIL;
  3276. ASSERT(SyntaxTree.Resolved IN type.state)
  3277. END
  3278. END FinalizeIndexDesignator;
  3279. BEGIN
  3280. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3281. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3282. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3283. (* copy all index list entries including a separator to the left bracket designator *)
  3284. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3285. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3286. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3287. END;
  3288. (* only resolve left bracket designator and use as final result *)
  3289. bracketDesignator.SetRelatedRhs(leftBracketDesignator.relatedRhs);
  3290. result := ResolveExpression(leftBracketDesignator)
  3291. ELSE
  3292. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3293. designator := ResolveDesignator(bracketDesignator.left);
  3294. type := designator.type.resolved;
  3295. indexDesignator := NIL;
  3296. (*!!! clean up *)
  3297. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  3298. OR (type IS SyntaxTree.RecordType)
  3299. THEN
  3300. result := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3301. IF result = NIL THEN
  3302. Error(bracketDesignator.position,"undefined operator");
  3303. result := SyntaxTree.invalidDesignator
  3304. END;
  3305. RETURN result;
  3306. END;
  3307. i := 0;
  3308. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3309. expression := bracketDesignator.parameters.GetExpression(i);
  3310. expression := ResolveExpression(expression);
  3311. bracketDesignator.parameters.SetExpression(i, expression);
  3312. IF expression = SyntaxTree.indexListSeparator THEN
  3313. (* finalize an existing index designator if needed *)
  3314. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3315. INC(i)
  3316. ELSE
  3317. (* do auto-dereferencing if needed *)
  3318. IF (type IS SyntaxTree.PointerType)
  3319. THEN
  3320. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  3321. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  3322. Error(expression.position, "forbidden range valued indexer over pointer to array");
  3323. designator := SyntaxTree.invalidDesignator;
  3324. type := SyntaxTree.invalidType
  3325. ELSE
  3326. FinalizeIndexDesignator;
  3327. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  3328. type := designator.type.resolved
  3329. END
  3330. END;
  3331. (* create a new index designator, if needed *)
  3332. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  3333. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  3334. indexDesignator.SetAssignable(designator.assignable);
  3335. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  3336. (* designator := indexDesignator *)
  3337. END;
  3338. IF type = SyntaxTree.invalidType THEN
  3339. (* error already handled *)
  3340. INC(i)
  3341. ELSIF type IS SyntaxTree.ArrayType THEN
  3342. (* indexing over an array *)
  3343. ASSERT(indexDesignator # NIL);
  3344. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  3345. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3346. INC(i)
  3347. ELSIF type IS SyntaxTree.StringType THEN
  3348. (* indexing over an array *)
  3349. ASSERT(indexDesignator # NIL);
  3350. AppendIndex(expression.position, indexDesignator, expression, type);
  3351. type := type(SyntaxTree.StringType).baseType.resolved;
  3352. INC(i)
  3353. ELSIF type IS SyntaxTree.MathArrayType THEN
  3354. (* indexing over a math array *)
  3355. ASSERT(indexDesignator # NIL);
  3356. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  3357. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3358. INC(i)
  3359. ELSE
  3360. Error(expression.position,"indexing over non-array type");
  3361. designator := SyntaxTree.invalidDesignator;
  3362. type := SyntaxTree.invalidType;
  3363. INC(i)
  3364. END
  3365. END
  3366. END;
  3367. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  3368. result := designator
  3369. END;
  3370. RETURN result;
  3371. END ResolveBracketDesignator;
  3372. (** check and resolve expression list
  3373. - resolve each expression in an expression list
  3374. - returns true if and only if all statements could have successfully been resolved
  3375. **)
  3376. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  3377. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  3378. BEGIN
  3379. result := TRUE;
  3380. FOR i := 0 TO expressionList.Length()-1 DO
  3381. expression := ResolveExpression(expressionList.GetExpression(i));
  3382. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  3383. expressionList.SetExpression(i,expression);
  3384. END;
  3385. RETURN result
  3386. END ExpressionList;
  3387. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  3388. BEGIN
  3389. type := type.resolved;
  3390. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  3391. RETURN TRUE
  3392. ELSIF system.CanPassInRegister # NIL THEN
  3393. RETURN system.CanPassInRegister(type);
  3394. ELSE
  3395. RETURN FALSE
  3396. END;
  3397. END CanPassInRegister;
  3398. (** return procedure call designator left(actualParameters)
  3399. - check realtime procedure call in realtime procedure
  3400. - check number of parameters
  3401. - check parameter compatibility
  3402. return invalidDesignator if error
  3403. **)
  3404. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  3405. VAR result: SyntaxTree.Designator;
  3406. numberFormalParameters, numberActualParameters: LONGINT;
  3407. formalType: SyntaxTree.ProcedureType;
  3408. formalParameter: SyntaxTree.Parameter;
  3409. actualParameter: SyntaxTree.Expression;
  3410. i: LONGINT;
  3411. self: SyntaxTree.Expression;
  3412. BEGIN
  3413. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  3414. result := SyntaxTree.invalidDesignator;
  3415. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  3416. numberFormalParameters := formalType.numberParameters;
  3417. numberActualParameters := actualParameters.Length();
  3418. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  3419. Error(position, "forbidden call of non-realtime procedure in realtime block");
  3420. END;
  3421. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  3422. self := left.left;
  3423. IF (self # NIL) & ~IsVariable(self) THEN
  3424. Error(self.position, "Non-variable expression on variable receiver");
  3425. END;
  3426. END;
  3427. IF ~ExpressionList(actualParameters) THEN
  3428. result := SyntaxTree.invalidDesignator
  3429. ELSE
  3430. IF numberActualParameters <= numberFormalParameters THEN
  3431. formalParameter := formalType.firstParameter;
  3432. FOR i := 0 TO numberActualParameters-1 DO
  3433. actualParameter := actualParameters.GetExpression(i);
  3434. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  3435. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  3436. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  3437. Error(position, "non-realtime actual parameter in context of realtime procedure");
  3438. ELSE
  3439. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  3440. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  3441. END;
  3442. actualParameters.SetExpression(i,actualParameter);
  3443. END;
  3444. formalParameter := formalParameter.nextParameter;
  3445. END;
  3446. WHILE (formalParameter # NIL) DO
  3447. IF formalParameter.defaultValue # NIL THEN
  3448. actualParameters.AddExpression(formalParameter.defaultValue);
  3449. formalParameter := formalParameter.nextParameter
  3450. ELSE
  3451. Error(position, "less actual than formal parameters");
  3452. formalParameter := NIL;
  3453. END;
  3454. END;
  3455. ELSE
  3456. Error(position, "more actual than formal parameters")
  3457. END;
  3458. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  3459. result.SetAssignable(FALSE);
  3460. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  3461. END;
  3462. RETURN result
  3463. END NewProcedureCallDesignator;
  3464. (**
  3465. builtin call designator generated in VisitParameterDesignator
  3466. -> nothing to be resolved
  3467. **)
  3468. PROCEDURE ResolveBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator): SyntaxTree.Expression;
  3469. BEGIN
  3470. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  3471. RETURN NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  3472. ELSIF ExpressionList(x.parameters) THEN
  3473. RETURN x;
  3474. END;
  3475. END ResolveBuiltinCallDesignator;
  3476. (** return true if x is a variable else return false and report error **)
  3477. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  3478. VAR result: BOOLEAN;
  3479. BEGIN
  3480. result := TRUE;
  3481. IF x = SyntaxTree.invalidExpression THEN
  3482. result := FALSE;
  3483. ELSIF ~IsVariable(x) THEN
  3484. Error(x.position,"non variable expression");
  3485. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  3486. result := FALSE;
  3487. END;
  3488. RETURN result
  3489. END CheckVariable;
  3490. (**
  3491. if expression x is of basic type then return true else report error and return false
  3492. **)
  3493. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  3494. VAR result: BOOLEAN;
  3495. BEGIN
  3496. result := FALSE;
  3497. IF x = SyntaxTree.invalidExpression THEN
  3498. ELSIF ~IsBasicType(x.type) THEN
  3499. Error(x.position,"is no basic type");
  3500. result := FALSE
  3501. ELSE result := TRUE
  3502. END;
  3503. RETURN result
  3504. END CheckBasicType;
  3505. (**
  3506. if expression x is of number type then return true else report error and return false
  3507. **)
  3508. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  3509. VAR result: BOOLEAN;
  3510. BEGIN
  3511. result := FALSE;
  3512. IF x = SyntaxTree.invalidExpression THEN
  3513. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  3514. Error(x.position,"is non number type");
  3515. ELSE result := TRUE
  3516. END;
  3517. RETURN result
  3518. END CheckNumberType;
  3519. (**
  3520. if expression x is of number or size type but not complex then return true else report error and return false
  3521. **)
  3522. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  3523. VAR result: BOOLEAN;
  3524. BEGIN
  3525. result := FALSE;
  3526. IF x = SyntaxTree.invalidExpression THEN
  3527. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  3528. Error(x.position,"is complex type");
  3529. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  3530. Error(x.position,"is non number type");
  3531. ELSE result := TRUE
  3532. END;
  3533. RETURN result
  3534. END CheckNonComplexNumberSizeType;
  3535. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  3536. VAR result: BOOLEAN; type: SyntaxTree.Type;
  3537. BEGIN
  3538. result := FALSE; type := x.type.resolved;
  3539. IF x = SyntaxTree.invalidExpression THEN
  3540. 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
  3541. TRACE(type.sizeInBits);
  3542. TRACE(system.addressType.sizeInBits);
  3543. Error(x.position,"is no address type");
  3544. ELSE result := TRUE
  3545. END;
  3546. RETURN result
  3547. END CheckAddressType;
  3548. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  3549. VAR result: BOOLEAN; type: SyntaxTree.Type;
  3550. BEGIN
  3551. result := FALSE; type := x.type.resolved;
  3552. IF x = SyntaxTree.invalidExpression THEN
  3553. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  3554. Error(x.position,"is no size type");
  3555. ELSE result := TRUE
  3556. END;
  3557. RETURN result
  3558. END CheckSizeType;
  3559. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  3560. VAR result: BOOLEAN; type: SyntaxTree.Type;
  3561. BEGIN
  3562. result := FALSE; type := x.type.resolved;
  3563. IF x = SyntaxTree.invalidExpression THEN
  3564. 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
  3565. Error(x.position,"is no object type");
  3566. ELSE result := TRUE
  3567. END;
  3568. RETURN result
  3569. END CheckObjectType;
  3570. (**
  3571. if expression x is of integer type then return true else report error and return false
  3572. **)
  3573. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  3574. VAR result: BOOLEAN; type: SyntaxTree.Type;
  3575. BEGIN
  3576. result := FALSE; type := x.type.resolved;
  3577. IF x = SyntaxTree.invalidExpression THEN
  3578. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  3579. Error(x.position,"is no integer type");
  3580. ELSE result := TRUE
  3581. END;
  3582. RETURN result
  3583. END CheckIntegerType;
  3584. (**
  3585. if expression x is of character type then return true else report error and return false
  3586. **)
  3587. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  3588. VAR result: BOOLEAN;
  3589. BEGIN
  3590. result := FALSE;
  3591. IF x = SyntaxTree.invalidExpression THEN
  3592. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  3593. Error(x.position,"is no character type");
  3594. ELSE result := TRUE
  3595. END;
  3596. RETURN result
  3597. END CheckCharacterType;
  3598. (**
  3599. if expression x is of real type then return true else report error and return false
  3600. **)
  3601. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  3602. VAR result: BOOLEAN;
  3603. BEGIN
  3604. result := FALSE;
  3605. IF x = SyntaxTree.invalidExpression THEN
  3606. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  3607. Error(x.position,"is no float type");
  3608. ELSE result := TRUE
  3609. END;
  3610. RETURN result
  3611. END CheckRealType;
  3612. (**
  3613. if expression x is of range type then return true else report error and return false
  3614. **)
  3615. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  3616. VAR result: BOOLEAN;
  3617. BEGIN
  3618. result := FALSE;
  3619. IF x = SyntaxTree.invalidExpression THEN
  3620. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  3621. Error(x.position,"is no range type");
  3622. ELSE result := TRUE
  3623. END;
  3624. RETURN result
  3625. END CheckRangeType;
  3626. (**
  3627. if expression x is of boolean type then return true else report error and return false
  3628. **)
  3629. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  3630. VAR result: BOOLEAN;
  3631. BEGIN
  3632. result := FALSE;
  3633. IF x = SyntaxTree.invalidExpression THEN
  3634. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  3635. Error(x.position,"is no boolean type");
  3636. ELSE result := TRUE
  3637. END;
  3638. RETURN result
  3639. END CheckBooleanType;
  3640. (**
  3641. if expression x is of set type then return true else report error and return false
  3642. **)
  3643. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  3644. VAR result: BOOLEAN;
  3645. BEGIN
  3646. result := FALSE;
  3647. IF x = SyntaxTree.invalidExpression THEN
  3648. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  3649. Error(x.position,"is no set type");
  3650. ELSE result := TRUE
  3651. END;
  3652. RETURN result
  3653. END CheckSetType;
  3654. (**
  3655. if expression x is of string or array of character type then return true else report error and return false
  3656. **)
  3657. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  3658. VAR result: BOOLEAN;
  3659. BEGIN
  3660. result := FALSE;
  3661. IF x = SyntaxTree.invalidExpression THEN
  3662. ELSIF ~IsStringType(x.type.resolved) THEN
  3663. Error(x.position,"is no string type");
  3664. ELSE result := TRUE
  3665. END;
  3666. RETURN result
  3667. END CheckStringType;
  3668. (**
  3669. if expression x is a type declaration type return true else report error and return false
  3670. **)
  3671. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  3672. VAR result: BOOLEAN;
  3673. BEGIN
  3674. result := FALSE;
  3675. IF x = SyntaxTree.invalidExpression THEN
  3676. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  3677. Error(x.position,"is not a type declaration");
  3678. ELSE result := TRUE
  3679. END;
  3680. RETURN result
  3681. END CheckTypeDeclarationType;
  3682. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  3683. VAR result: BOOLEAN;
  3684. BEGIN
  3685. result := FALSE;
  3686. IF x = SyntaxTree.invalidExpression THEN
  3687. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  3688. result := TRUE;
  3689. value := x.resolved(SyntaxTree.IntegerValue).value;
  3690. ELSE
  3691. Error(x.position,"expression is not an integer constant");
  3692. END;
  3693. RETURN result;
  3694. END CheckIntegerValue;
  3695. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  3696. VAR result: BOOLEAN;
  3697. BEGIN
  3698. result := FALSE;
  3699. IF x = SyntaxTree.invalidExpression THEN
  3700. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  3701. result := TRUE;
  3702. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  3703. ELSE
  3704. Error(x.position,"expression is not an integer constant");
  3705. END;
  3706. RETURN result;
  3707. END CheckStringValue;
  3708. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  3709. BEGIN
  3710. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  3711. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, maxSizeInBits)
  3712. ELSE
  3713. RETURN FALSE
  3714. END;
  3715. END IsUnsignedValue;
  3716. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  3717. BEGIN
  3718. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  3719. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, system.addressType.sizeInBits)
  3720. ELSE
  3721. RETURN FALSE
  3722. END
  3723. END IsAddressValue;
  3724. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  3725. BEGIN
  3726. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  3727. END IsAddressExpression;
  3728. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  3729. BEGIN
  3730. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  3731. END IsSizeExpression;
  3732. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  3733. VAR result: BOOLEAN;
  3734. BEGIN
  3735. result := FALSE;
  3736. IF x = SyntaxTree.invalidExpression THEN
  3737. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  3738. result := TRUE;
  3739. value := x.resolved(SyntaxTree.EnumerationValue).value;
  3740. ELSE
  3741. Error(x.position,"expression is not an integer constant");
  3742. END;
  3743. RETURN result;
  3744. END CheckEnumerationValue;
  3745. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  3746. VAR result: BOOLEAN;
  3747. BEGIN
  3748. result := FALSE;
  3749. IF x = SyntaxTree.invalidExpression THEN
  3750. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  3751. result := TRUE;
  3752. value := x.resolved(SyntaxTree.CharacterValue).value;
  3753. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  3754. result := TRUE;
  3755. value := x.resolved(SyntaxTree.StringValue).value[0];
  3756. ELSE
  3757. Error(x.position,"expression is not a character constant");
  3758. END;
  3759. RETURN result;
  3760. END CheckCharacterValue;
  3761. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer; includeZero: BOOLEAN): BOOLEAN;
  3762. VAR result: BOOLEAN;
  3763. BEGIN
  3764. result := FALSE;
  3765. IF x = SyntaxTree.invalidExpression THEN
  3766. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  3767. value := x.resolved(SyntaxTree.IntegerValue).value;
  3768. IF (value > 0) OR includeZero & (value = 0) THEN
  3769. result := TRUE;
  3770. ELSE
  3771. Error(x.position,"integer is not positive");
  3772. END
  3773. ELSE
  3774. Error(x.position,"expression is not an integer constant");
  3775. END;
  3776. RETURN result;
  3777. END CheckPositiveIntegerValue;
  3778. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  3779. VAR type: SyntaxTree.Type; result: BOOLEAN;
  3780. BEGIN
  3781. result := FALSE;
  3782. IF x = SyntaxTree.invalidExpression THEN
  3783. ELSE
  3784. type := x.type.resolved;
  3785. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  3786. portType := type(SyntaxTree.PortType);
  3787. result := TRUE
  3788. ELSE
  3789. Error(x.position,"no port type");
  3790. END;
  3791. END;
  3792. RETURN result
  3793. END CheckPortType;
  3794. (* move to builtin procedure call statement ?
  3795. remove builtin procedure call designator ?
  3796. *)
  3797. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  3798. VAR
  3799. numberActualParameters,numberFormalParameters: LONGINT;
  3800. formalParameter: SyntaxTree.Parameter;
  3801. actualParameter: SyntaxTree.Expression;
  3802. procedureType: SyntaxTree.ProcedureType;
  3803. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  3804. inPort, outPort: SyntaxTree.PortType;
  3805. constructor: SyntaxTree.Procedure;
  3806. type0,type1,type2: SyntaxTree.Type;
  3807. type,base,parameterType: SyntaxTree.Type;
  3808. arrayType: SyntaxTree.ArrayType;
  3809. i: LONGINT; i0, i1, value: Basic.Integer;
  3810. r,r0,r1,im: LONGREAL;
  3811. c: CHAR;
  3812. id: LONGINT;
  3813. b: BOOLEAN;
  3814. first: LONGINT;
  3815. mathArrayType: SyntaxTree.MathArrayType;
  3816. customBuiltin: SyntaxTree.CustomBuiltin;
  3817. PROCEDURE CheckArity(from,to: Basic.Integer): BOOLEAN;
  3818. VAR resultB: BOOLEAN;
  3819. BEGIN
  3820. IF numberActualParameters < from THEN
  3821. Error(position, "less actual than formal parameters");
  3822. result := SyntaxTree.invalidExpression;
  3823. resultB := FALSE;
  3824. ELSIF numberActualParameters > to THEN
  3825. Error(position, "more actual than formal parameters");
  3826. result := SyntaxTree.invalidExpression;
  3827. resultB := FALSE;
  3828. ELSE
  3829. resultB := TRUE;
  3830. END;
  3831. RETURN resultB
  3832. END CheckArity;
  3833. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  3834. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  3835. BEGIN
  3836. WHILE modifier # NIL DO
  3837. symbol := cellType.FindProperty(modifier.identifier);
  3838. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  3839. propertyType := symbol.type.resolved;
  3840. modifierType := modifier.expression.type.resolved;
  3841. IF ~CompatibleTo(system, modifierType, propertyType) &
  3842. ~(
  3843. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  3844. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  3845. Error(modifier.position,"incompatible to cell property");
  3846. END;
  3847. ELSE
  3848. Error(modifier.position, "undefined property");
  3849. END;
  3850. modifier := modifier.nextModifier;
  3851. END;
  3852. END CheckModifiers;
  3853. BEGIN
  3854. type := NIL; result := NIL;
  3855. type0 := NIL; type1 := NIL; type2 := NIL;
  3856. numberActualParameters := actualParameters.Length();
  3857. IF numberActualParameters>0 THEN
  3858. parameter0 := actualParameters.GetExpression(0);
  3859. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  3860. Error(parameter0.position,"forbidden type-less argument");
  3861. result := SyntaxTree.invalidExpression
  3862. END
  3863. END;
  3864. IF numberActualParameters >1 THEN
  3865. parameter1 := actualParameters.GetExpression(1);
  3866. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  3867. ELSE
  3868. Error(parameter1.position,"forbidden type-less argument");
  3869. result := SyntaxTree.invalidExpression
  3870. END
  3871. END;
  3872. IF numberActualParameters >2 THEN
  3873. parameter2 := actualParameters.GetExpression(2);
  3874. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  3875. ELSE
  3876. Error(parameter2.position,"forbidden type-less argument");
  3877. result := SyntaxTree.invalidExpression
  3878. END
  3879. END;
  3880. IF returnType # NIL THEN
  3881. id := Global.New;
  3882. result := NIL;
  3883. ELSE
  3884. id := builtin.id;
  3885. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  3886. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  3887. END;
  3888. END;
  3889. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  3890. ELSIF result # NIL THEN type := result.type (* operator *)
  3891. ELSE
  3892. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  3893. result(SyntaxTree.Designator).SetLeft(left);
  3894. IF returnType # NIL THEN
  3895. type := returnType;
  3896. END;
  3897. (* ---- ASSERT ----- *)
  3898. IF (id = Global.Assert) & CheckArity(1,2) THEN
  3899. IF CheckBooleanType(parameter0) THEN
  3900. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  3901. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  3902. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  3903. *)
  3904. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  3905. Error(position, "static assertion failed");
  3906. END;
  3907. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  3908. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  3909. rules imposed by the architecture / current runtime
  3910. *)
  3911. END;
  3912. END;
  3913. (* ---- COPY ----- *)
  3914. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  3915. IF~IsStringType(type0) THEN
  3916. Error(parameter0.position,"no string type");
  3917. END;
  3918. IF ~IsStringType(type1) THEN
  3919. Error(parameter1.position,"no string type");
  3920. ELSIF CheckVariable(parameter1) THEN
  3921. IF (type0 IS SyntaxTree.StringType) THEN
  3922. arrayType := type1(SyntaxTree.ArrayType);
  3923. IF arrayType.form = SyntaxTree.Static THEN
  3924. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  3925. Error(position, "destination length smaller than source length")
  3926. END;
  3927. END;
  3928. END;
  3929. END;
  3930. (* ---- INC, DEC----- *)
  3931. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  3932. IF numberActualParameters = 1 THEN
  3933. parameter1 :=Global.NewIntegerValue(system,position,1);
  3934. actualParameters.AddExpression(parameter1);
  3935. END;
  3936. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  3937. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  3938. Error(position, "incompatible increment");
  3939. ELSE
  3940. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  3941. actualParameters.SetExpression(1,parameter1);
  3942. END;
  3943. END;
  3944. (* ---- EXCL, INCL----- *)
  3945. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  3946. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  3947. IF IsIntegerValue(parameter1,i0) THEN
  3948. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  3949. Error(position, "parameter out of SET range")
  3950. END;
  3951. END;
  3952. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  3953. actualParameters.SetExpression(1,parameter1);
  3954. END;
  3955. (* ---- HALT, SYSTEM.HALT ----- *)
  3956. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  3957. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  3958. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  3959. rules imposed by the architecture / current runtime
  3960. *)
  3961. END;
  3962. (* ---- WAIT ----- *)
  3963. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  3964. IF CheckObjectType(parameter0) THEN
  3965. END;
  3966. (* ---- NEW ----- *)
  3967. ELSIF (id = Global.New) THEN
  3968. IF returnType # NIL THEN
  3969. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  3970. ELSE
  3971. first := 1;
  3972. END;
  3973. IF CheckArity(first,Infinity) THEN
  3974. IF currentIsRealtime THEN
  3975. Error(position, "forbidden new in realtime block");
  3976. END;
  3977. IF IsUnsafePointer(type0) THEN
  3978. Error(position, "forbidden new on unsafe pointer");
  3979. END;
  3980. (* check constructor *)
  3981. IF (first =0) OR CheckVariable(parameter0) THEN
  3982. IF type0 IS SyntaxTree.PointerType THEN
  3983. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  3984. ELSIF type0 IS SyntaxTree.CellType THEN
  3985. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  3986. ELSE
  3987. Error(position, "forbidden new on value type");
  3988. END;
  3989. IF type0 IS SyntaxTree.ArrayType THEN
  3990. arrayType := type0(SyntaxTree.ArrayType);
  3991. IF arrayType.form = SyntaxTree.Static THEN
  3992. i := first
  3993. ELSIF arrayType.form = SyntaxTree.Open THEN
  3994. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  3995. ELSE HALT(100)
  3996. END;
  3997. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  3998. i := first;
  3999. REPEAT
  4000. actualParameter := actualParameters.GetExpression(i);
  4001. IF CheckSizeType(actualParameter) THEN
  4002. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4003. actualParameters.SetExpression(i,actualParameter);
  4004. END;
  4005. INC(i);
  4006. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4007. END;
  4008. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4009. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4010. Error(position, "forbidden new on abstract object");
  4011. END;
  4012. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4013. IF constructor = NIL THEN
  4014. IF CheckArity(first,first) THEN END;
  4015. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4016. Error(position, "new on object with hidden constructor");
  4017. ELSE
  4018. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4019. numberFormalParameters := procedureType.numberParameters;
  4020. IF numberActualParameters-first <= numberFormalParameters THEN
  4021. formalParameter := procedureType.firstParameter;
  4022. FOR i := first TO numberActualParameters-1 DO
  4023. actualParameter := actualParameters.GetExpression(i);
  4024. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4025. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4026. ELSE
  4027. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4028. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4029. END;
  4030. actualParameters.SetExpression(i,actualParameter);
  4031. END;
  4032. formalParameter := formalParameter.nextParameter;
  4033. END;
  4034. WHILE (formalParameter # NIL) DO
  4035. IF formalParameter.defaultValue # NIL THEN
  4036. actualParameters.AddExpression(formalParameter.defaultValue);
  4037. formalParameter := formalParameter.nextParameter
  4038. ELSE
  4039. Error(position, "less actual than formal parameters");
  4040. formalParameter := NIL;
  4041. END;
  4042. END;
  4043. ELSE
  4044. Error(position, "more actual than formal parameters")
  4045. END;
  4046. END;
  4047. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4048. mathArrayType := type0(SyntaxTree.MathArrayType);
  4049. IF mathArrayType.form = SyntaxTree.Static THEN
  4050. Error(position, "new on static array");
  4051. ELSE
  4052. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4053. i0 := first+1; i1 := Infinity;
  4054. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4055. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4056. i1 := i0;
  4057. ELSE HALT(100);
  4058. END;
  4059. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4060. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4061. base := ArrayBase(type0,MAX(LONGINT));
  4062. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4063. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4064. IF ~CompatibleTo(system,type0,parameterType) THEN
  4065. Error(parameter0.position,"incompatible parameter in new");
  4066. result := SyntaxTree.invalidExpression;
  4067. ELSE
  4068. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4069. END;
  4070. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4071. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4072. IF ~CompatibleTo(system,type1,parameterType) THEN
  4073. Error(parameter1.position,"parameter incompatible to math array len type");
  4074. result := SyntaxTree.invalidExpression;
  4075. ELSE
  4076. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4077. END;
  4078. ELSE
  4079. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4080. i := first;
  4081. REPEAT
  4082. actualParameter := actualParameters.GetExpression(i);
  4083. IF CheckSizeType(actualParameter) THEN
  4084. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4085. actualParameters.SetExpression(i,actualParameter);
  4086. END;
  4087. INC(i);
  4088. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4089. END;
  4090. END;
  4091. END;
  4092. ELSIF type0 IS SyntaxTree.CellType THEN
  4093. IF ~(currentIsCellNet) THEN
  4094. Error(position, "cell allocation outside activeCells ");
  4095. ELSE
  4096. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4097. IF (constructor = NIL) & CheckArity(1,1) THEN
  4098. (* ok *)
  4099. ELSE
  4100. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4101. numberFormalParameters := procedureType.numberParameters;
  4102. DEC(numberActualParameters);
  4103. IF numberActualParameters <= numberFormalParameters THEN
  4104. formalParameter := procedureType.firstParameter;
  4105. FOR i := first TO numberActualParameters DO
  4106. actualParameter := actualParameters.GetExpression(i);
  4107. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4108. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4109. ELSE
  4110. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4111. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4112. END;
  4113. actualParameters.SetExpression(i,actualParameter);
  4114. END;
  4115. formalParameter := formalParameter.nextParameter;
  4116. END;
  4117. WHILE (formalParameter # NIL) DO
  4118. IF formalParameter.defaultValue # NIL THEN
  4119. actualParameters.AddExpression(formalParameter.defaultValue);
  4120. formalParameter := formalParameter.nextParameter
  4121. ELSE
  4122. Error(position, "less actual than formal parameters");
  4123. formalParameter := NIL;
  4124. END;
  4125. END;
  4126. ELSE
  4127. Error(position, "more actual than formal parameters")
  4128. END;
  4129. END;
  4130. END;
  4131. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4132. activeCellsStatement := TRUE;
  4133. ELSE
  4134. Error(position, "cannot be allocated");
  4135. END;
  4136. END;
  4137. END;
  4138. (* ---- DISPOSE ----- *)
  4139. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4140. IF ~IsPointerType(parameter0.type) THEN
  4141. Error(parameter0.position,"is not a pointer")
  4142. ELSIF ~IsDisposable(parameter0.type) THEN
  4143. Error(parameter0.position,"is not disposable")
  4144. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4145. END
  4146. (* ---- GETPROCEDURE ----- *)
  4147. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4148. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4149. IF CheckVariable(parameter2) THEN
  4150. IF ~GetProcedureAllowed(parameter2.type) THEN
  4151. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  4152. END;
  4153. END;
  4154. END;
  4155. (* ---- ABS ----- *)
  4156. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4157. (* note: ABS on complex numbers is done using overloading *)
  4158. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4159. type := type0;
  4160. IF IsIntegerValue(parameter0,i0) THEN
  4161. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4162. type := Global.GetIntegerType(system,ABS(i0));
  4163. ELSIF IsRealValue(parameter0,r) THEN
  4164. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4165. END;
  4166. ELSE
  4167. type := SyntaxTree.invalidType;
  4168. END;
  4169. (* ---- ASH, ASR ----- *)
  4170. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4171. type := type0;
  4172. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4173. (*
  4174. ConvertOperands(parameter0,parameter1); (* same type *)
  4175. *)
  4176. type := parameter0.type;
  4177. IF IsIntegerValue(parameter0,i0) THEN
  4178. IF IsIntegerValue(parameter1,i1) THEN
  4179. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4180. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4181. result := ResolveExpression(result);
  4182. type := Global.GetIntegerType(system,i0);
  4183. END;
  4184. END;
  4185. IF type.resolved.sizeInBits < 32 THEN
  4186. type := system.longintType;
  4187. END;
  4188. (*!compatibility with release, remove when resolved
  4189. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4190. *)
  4191. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4192. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4193. actualParameters.SetExpression(0,parameter0);
  4194. actualParameters.SetExpression(1,parameter1);
  4195. END;
  4196. (* ---- CAP ----- *)
  4197. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4198. type := system.characterType;
  4199. IF CheckCharacterType (parameter0) THEN
  4200. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4201. actualParameters.SetExpression(0,parameter0);
  4202. IF IsCharacterValue(parameter0,c) THEN
  4203. IF (c <= "z") & (c >= "a") THEN
  4204. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4205. ELSE
  4206. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4207. END;
  4208. END;
  4209. END;
  4210. (* ---- CHR ----- *)
  4211. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4212. IF id = Global.Chr THEN
  4213. type := system.characterType
  4214. ELSE
  4215. type := system.characterType32
  4216. END;
  4217. IF CheckIntegerType(parameter0) THEN
  4218. IF IsIntegerValue(parameter0,i0) THEN
  4219. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4220. result := ResolveExpression(result);
  4221. ELSE
  4222. (*
  4223. result := NewConversion(parameter0.position,parameter0,type);
  4224. *)
  4225. END;
  4226. END
  4227. (* ---- ENTIER ----- *)
  4228. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4229. type := system.longintType;
  4230. IF CheckRealType(parameter0) THEN
  4231. IF IsRealValue(parameter0,r) THEN
  4232. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4233. type := Global.GetIntegerType(system,ENTIER(r));
  4234. END
  4235. END;
  4236. (* ---- ENTIERH ----- *)
  4237. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4238. type := system.hugeintType;
  4239. IF CheckRealType(parameter0) THEN
  4240. IF IsRealValue(parameter0,r) THEN
  4241. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4242. END
  4243. END;
  4244. (* ---- LEN ----- *)
  4245. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4246. type := system.lenType;
  4247. base := type0;
  4248. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4249. IF base(SyntaxTree.PointerType).isUnsafe THEN
  4250. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  4251. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  4252. Error(position, "forbidden len on unsafe pointer");
  4253. END;
  4254. type0 := base;
  4255. ELSE
  4256. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  4257. type0 := parameter0.type.resolved;
  4258. actualParameters.SetExpression(0,parameter0);
  4259. base := type0;
  4260. END;
  4261. END;
  4262. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4263. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4264. IF i1 < 0 THEN
  4265. Error(position, "invalid dimension");
  4266. base := SyntaxTree.invalidType;
  4267. ELSE
  4268. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  4269. IF (base # NIL) & Indexable(base) THEN
  4270. ELSE
  4271. Error(position, "len on no array");
  4272. IF VerboseErrorMessage THEN
  4273. Printout.Info("base",base);
  4274. END;
  4275. base := SyntaxTree.invalidType;
  4276. END;
  4277. END;
  4278. IF numberActualParameters=2 THEN
  4279. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  4280. actualParameters.SetExpression(1,parameter1);
  4281. ELSIF base IS SyntaxTree.MathArrayType THEN
  4282. Error(position, "missing dimension specification");
  4283. END;
  4284. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  4285. IF base IS SyntaxTree.ArrayType THEN
  4286. arrayType := base(SyntaxTree.ArrayType);
  4287. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  4288. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  4289. result := Global.NewIntegerValue(system,position,value);
  4290. type := result.type;(* arrayType.length.type;*)
  4291. ASSERT(type # NIL);
  4292. END;
  4293. ELSIF base IS SyntaxTree.MathArrayType THEN
  4294. mathArrayType := base(SyntaxTree.MathArrayType);
  4295. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  4296. result := Global.NewIntegerValue(system,position,value);
  4297. type := result.type;
  4298. (*
  4299. type := mathArrayType.length.type;
  4300. *)
  4301. ASSERT(type # NIL);
  4302. END;
  4303. END;
  4304. END;
  4305. ELSE
  4306. type := system.lenType;
  4307. END;
  4308. (* ---- FIRST ---- *)
  4309. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  4310. type := system.lenType;
  4311. IF CheckRangeType(parameter0) THEN END;
  4312. result.SetAssignable(parameter0.assignable)
  4313. (* ---- LAST ---- *)
  4314. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  4315. type := system.lenType;
  4316. IF CheckRangeType(parameter0) THEN END;
  4317. result.SetAssignable(parameter0.assignable)
  4318. (* ---- STEP ---- *)
  4319. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  4320. type := system.lenType;
  4321. IF CheckRangeType(parameter0) THEN END;
  4322. result.SetAssignable(parameter0.assignable)
  4323. (* ---- RE ---- *)
  4324. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  4325. IF CheckNumberType(parameter0) THEN
  4326. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4327. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4328. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  4329. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  4330. type := parameter0.type
  4331. ELSE
  4332. type := system.realType
  4333. END
  4334. END;
  4335. result.SetAssignable(parameter0.assignable)
  4336. (* ---- IM ---- *)
  4337. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  4338. IF CheckNumberType(parameter0) THEN
  4339. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4340. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4341. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  4342. ELSE
  4343. type := system.realType;
  4344. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  4345. END
  4346. END;
  4347. result.SetAssignable(parameter0.assignable)
  4348. (* ---- MAX ----- *)
  4349. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  4350. IF numberActualParameters = 1 THEN
  4351. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  4352. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  4353. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  4354. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  4355. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  4356. *)
  4357. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  4358. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  4359. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  4360. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  4361. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  4362. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  4363. END;
  4364. ELSE
  4365. Error(parameter0.position,"is not a type symbol");
  4366. END
  4367. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  4368. ConvertOperands(parameter0,parameter1);
  4369. actualParameters.SetExpression(0,parameter0);
  4370. actualParameters.SetExpression(1,parameter1);
  4371. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  4372. IF r0 > r1 THEN result.SetResolved(parameter0.resolved)
  4373. ELSE result.SetResolved(parameter0.resolved)
  4374. END;
  4375. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  4376. IF i0 > i1 THEN result.SetResolved(parameter0.resolved)
  4377. ELSE result.SetResolved(parameter1.resolved)
  4378. END;
  4379. END;
  4380. type := parameter0.type;
  4381. ELSE type := SyntaxTree.invalidType;
  4382. END;
  4383. (* ---- MIN ----- *)
  4384. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  4385. IF numberActualParameters = 1 THEN
  4386. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  4387. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  4388. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  4389. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  4390. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  4391. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  4392. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  4393. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  4394. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  4395. END;
  4396. ELSE
  4397. Error(parameter0.position,"is not a type symbol");
  4398. END
  4399. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  4400. ConvertOperands(parameter0,parameter1);
  4401. actualParameters.SetExpression(0,parameter0);
  4402. actualParameters.SetExpression(1,parameter1);
  4403. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  4404. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  4405. ELSE result.SetResolved(parameter1.resolved)
  4406. END;
  4407. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  4408. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  4409. ELSE result.SetResolved(parameter1.resolved)
  4410. END;
  4411. END;
  4412. type := parameter0.type;
  4413. ELSE type := SyntaxTree.invalidType;
  4414. END;
  4415. (* ---- ODD ----- *)
  4416. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  4417. type := system.booleanType;
  4418. IF CheckIntegerType(parameter0) THEN
  4419. IF IsIntegerValue(parameter0,i0) THEN
  4420. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  4421. type := system.booleanType;
  4422. END;
  4423. END;
  4424. (* ---- ORD ----- *)
  4425. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  4426. IF id = Global.Ord THEN
  4427. type := system.integerType;
  4428. ELSE
  4429. type := system.longintType;
  4430. END;
  4431. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  4432. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  4433. actualParameters.SetExpression(0,parameter0);
  4434. (* IF CheckCharacterType(parameter0) THEN*)
  4435. IF IsCharacterValue(parameter0,c)THEN
  4436. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  4437. type := Global.GetSignedIntegerType(system,ORD(c));
  4438. END;
  4439. ELSE Error(parameter0.position, "incompatible parameter");
  4440. END;
  4441. (* ---- SHORT ----- *)
  4442. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  4443. type := type0;
  4444. IF IsSignedIntegerType(type) THEN
  4445. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  4446. ELSIF type = system.integerType THEN type := system.shortintType
  4447. ELSIF type = system.longintType THEN type := system.integerType
  4448. ELSIF type = system.hugeintType THEN type:= system.longintType
  4449. ELSE
  4450. CASE type.sizeInBits OF
  4451. 16: type := Global.Integer8
  4452. |32: type := Global.Integer16
  4453. |64: type := Global.Integer32
  4454. END;
  4455. END;
  4456. ELSIF IsUnsignedIntegerType(type) THEN
  4457. IF (type.sizeInBits = 8) OR (type = Global.Unsigned8) THEN Error(parameter0.position,"short not applicable")
  4458. ELSE
  4459. CASE type.sizeInBits OF
  4460. 16: type := Global.Unsigned8
  4461. |32: type := Global.Unsigned16
  4462. |64: type := Global.Unsigned32
  4463. END;
  4464. END;
  4465. ELSIF type IS SyntaxTree.FloatType THEN
  4466. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  4467. ELSIF type = system.longrealType THEN type := system.realType
  4468. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  4469. END;
  4470. ELSIF type IS SyntaxTree.ComplexType THEN
  4471. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  4472. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  4473. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  4474. END;
  4475. ELSE
  4476. Error(parameter0.position,"short not applicable")
  4477. END;
  4478. IF (parameter0.resolved # NIL) THEN
  4479. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  4480. IF parameter0 IS SyntaxTree.Value THEN
  4481. result.SetResolved(parameter0(SyntaxTree.Value));
  4482. END;
  4483. END;
  4484. (* ---- LONG ----- *)
  4485. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  4486. type := type0;
  4487. IF IsSignedIntegerType(type) THEN
  4488. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  4489. ELSIF type = system.longintType THEN type := system.hugeintType
  4490. ELSIF type = system.integerType THEN type := system.longintType
  4491. ELSIF type = system.shortintType THEN type := system.integerType
  4492. ELSE
  4493. CASE type.sizeInBits OF
  4494. 8: type := Global.Integer16
  4495. |16: type := Global.Integer32
  4496. |32: type := Global.Integer64
  4497. END;
  4498. END;
  4499. ELSIF IsUnsignedIntegerType(type) THEN
  4500. IF (type.sizeInBits = 64) OR (type = Global.Unsigned64) THEN Error(parameter0.position,"long not applicable")
  4501. ELSE
  4502. CASE type.sizeInBits OF
  4503. 8: type := Global.Unsigned16
  4504. |16: type := Global.Unsigned32
  4505. |32: type := Global.Unsigned64
  4506. END;
  4507. END;
  4508. ELSIF type IS SyntaxTree.FloatType THEN
  4509. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  4510. ELSIF type= system.realType THEN type := system.longrealType
  4511. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  4512. END;
  4513. ELSIF type IS SyntaxTree.ComplexType THEN
  4514. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  4515. ELSIF type = system.complexType THEN type := system.longcomplexType
  4516. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  4517. END;
  4518. ELSE
  4519. Error(parameter0.position,"long not applicable")
  4520. END;
  4521. IF (parameter0.resolved # NIL) THEN
  4522. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  4523. IF parameter0 IS SyntaxTree.Value THEN
  4524. result.SetResolved(parameter0(SyntaxTree.Value));
  4525. END;
  4526. END;
  4527. (* ---- SIZE OF ----- *)
  4528. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  4529. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  4530. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  4531. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  4532. type := system.integerType;
  4533. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  4534. ELSE
  4535. (* for variables, system sizeof could represent the physically occupied size
  4536. determined via the type descriptor, implement that ? *)
  4537. Error(parameter0.position,"is not a type symbol");
  4538. END
  4539. (* ---- SYSTEM.TRACE -----*)
  4540. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  4541. FOR i := 0 TO numberActualParameters-1 DO
  4542. parameter0 := actualParameters.GetExpression(i);
  4543. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  4544. Error(parameter0.position,"incompatible parameter");
  4545. END;
  4546. END;
  4547. (* remaining issues can only be tested in backend *)
  4548. (* ---- ADDRESSOF----- *)
  4549. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  4550. IF HasAddress(parameter0) THEN
  4551. type := system.addressType;
  4552. ELSE
  4553. type := SyntaxTree.invalidType;
  4554. Error(parameter0.position,"has no address");
  4555. END;
  4556. (* ---- BIT ----- *)
  4557. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  4558. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  4559. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4560. actualParameters.SetExpression(0,parameter0);
  4561. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  4562. actualParameters.SetExpression(1,parameter1);
  4563. END;
  4564. type := system.booleanType;
  4565. (* ----- MSK ---- *)
  4566. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  4567. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4568. ConvertOperands(parameter0,parameter1);
  4569. actualParameters.SetExpression(0,parameter0);
  4570. actualParameters.SetExpression(1,parameter1);
  4571. END;
  4572. type := parameter0.type;
  4573. (* ---- SYSTEM.GET64 ----- *)
  4574. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  4575. IF CheckAddressType(parameter0) THEN
  4576. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4577. actualParameters.SetExpression(0,parameter0);
  4578. END;
  4579. type := system.hugeintType;
  4580. (* ---- SYSTEM.GET32 ----- *)
  4581. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  4582. IF CheckAddressType(parameter0) THEN
  4583. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4584. actualParameters.SetExpression(0,parameter0);
  4585. END;
  4586. type := system.longintType;
  4587. (* ---- SYSTEM.GET16 ----- *)
  4588. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  4589. IF CheckAddressType(parameter0) THEN
  4590. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4591. actualParameters.SetExpression(0,parameter0);
  4592. END;
  4593. type := system.integerType;
  4594. (* ---- SYSTEM.GET8 ----- *)
  4595. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  4596. IF CheckAddressType(parameter0) THEN
  4597. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4598. actualParameters.SetExpression(0,parameter0);
  4599. END;
  4600. type := system.shortintType;
  4601. (* ---- SYSTEM.GetStackPointer ----- *)
  4602. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  4603. type := system.addressType;
  4604. (* ---- SYSTEM.GetFramePointer ----- *)
  4605. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  4606. type := system.addressType;
  4607. (* ---- SYSTEM.GetActivity ----- *)
  4608. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  4609. type := system.objectType;
  4610. (* ---- SYSTEM.SetStackPointer ----- *)
  4611. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  4612. IF CheckAddressType(parameter0) THEN
  4613. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4614. actualParameters.SetExpression(0,parameter0);
  4615. END;
  4616. (* ---- SYSTEM.SetFramePointer ----- *)
  4617. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  4618. IF CheckAddressType(parameter0) THEN
  4619. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4620. actualParameters.SetExpression(0,parameter0);
  4621. END;
  4622. (* ---- SYSTEM.SetActivity ----- *)
  4623. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  4624. IF CheckObjectType(parameter0) THEN
  4625. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4626. actualParameters.SetExpression(0,parameter0);
  4627. END;
  4628. (* ---- LSH, LSL, ROT, ROR ----- *)
  4629. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  4630. type := type0;
  4631. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4632. actualParameters.SetExpression(1, parameter1);
  4633. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  4634. IF id = Global.Lsh THEN
  4635. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  4636. ELSIF id = Global.Rot THEN
  4637. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  4638. ELSIF id = Global.Ror THEN
  4639. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  4640. END;
  4641. END;
  4642. (* ---- SYSTEM.VAL ----- *)
  4643. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  4644. IF CheckTypeDeclarationType(parameter0) THEN
  4645. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  4646. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  4647. result := SyntaxTree.invalidExpression;
  4648. Error(parameter0.position,"is no basic type");
  4649. ELSE
  4650. IF (parameter1.resolved # NIL) THEN
  4651. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  4652. IF parameter0 IS SyntaxTree.Value THEN
  4653. result.SetResolved(parameter0(SyntaxTree.Value));
  4654. END;
  4655. END;
  4656. result.SetAssignable(parameter1.assignable);
  4657. END;
  4658. END;
  4659. (* ---- SYSTEM.GET ----- *)
  4660. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  4661. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  4662. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4663. actualParameters.SetExpression(0,parameter0);
  4664. END;
  4665. (* ---- SYSTEM.PUT ----- *)
  4666. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  4667. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4668. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4669. actualParameters.SetExpression(0,parameter0);
  4670. END;
  4671. (* ---- SYSTEM.PUT64 ----- *)
  4672. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  4673. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4674. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4675. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  4676. actualParameters.SetExpression(0,parameter0);
  4677. actualParameters.SetExpression(1,parameter1);
  4678. END;
  4679. (* ---- SYSTEM.PUT32 ----- *)
  4680. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  4681. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4682. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4683. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4684. actualParameters.SetExpression(0,parameter0);
  4685. actualParameters.SetExpression(1,parameter1);
  4686. END;
  4687. (* ---- SYSTEM.PUT16 ----- *)
  4688. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  4689. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4690. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4691. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  4692. actualParameters.SetExpression(0,parameter0);
  4693. actualParameters.SetExpression(1,parameter1);
  4694. END;
  4695. (* ---- SYSTEM.PUT8 ----- *)
  4696. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  4697. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  4698. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  4699. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  4700. actualParameters.SetExpression(0,parameter0);
  4701. actualParameters.SetExpression(1,parameter1);
  4702. END;
  4703. (* ---- SYSTEM.MOVE ----- *)
  4704. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  4705. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  4706. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  4707. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  4708. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  4709. actualParameters.SetExpression(0,parameter0);
  4710. actualParameters.SetExpression(1,parameter1);
  4711. actualParameters.SetExpression(2,parameter2);
  4712. END;
  4713. (* ---- SYSTEM.NEW ----- *)
  4714. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  4715. IF ~IsPointerType(parameter0.type) THEN
  4716. Error(parameter0.position,"is not a pointer")
  4717. ELSIF CheckSizeType(parameter1) THEN
  4718. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  4719. actualParameters.SetExpression(1,parameter1);
  4720. END;
  4721. (* ----SYSTEM.REF ---- *)
  4722. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  4723. type := system.addressType
  4724. (* ---- INCR ----- *)
  4725. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  4726. type := system.lenType;
  4727. base := type0;
  4728. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  4729. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4730. IF i1 < 0 THEN
  4731. Error(position, "invalid dimension");
  4732. base := SyntaxTree.invalidType;
  4733. ELSE
  4734. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  4735. IF (base # NIL) & Indexable(base) THEN
  4736. ELSE
  4737. Error(position, "len on no array");
  4738. IF VerboseErrorMessage THEN
  4739. Printout.Info("base",base);
  4740. END;
  4741. base := SyntaxTree.invalidType;
  4742. END;
  4743. END;
  4744. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  4745. actualParameters.SetExpression(1,parameter1);
  4746. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  4747. mathArrayType := base(SyntaxTree.MathArrayType);
  4748. IF (mathArrayType.form = SyntaxTree.Static) THEN
  4749. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  4750. type := system.lenType;
  4751. END;
  4752. END;
  4753. ELSE
  4754. type := system.lenType;
  4755. END;
  4756. (* ---- SUM ----- *)
  4757. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  4758. Error(position, "sum operator not applicable");
  4759. (* ---- ALL ----- *)
  4760. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  4761. Error(position, "all operator not applicable");
  4762. (* ---- DIM ----- *)
  4763. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  4764. type := system.lenType;
  4765. IF type0 IS SyntaxTree.MathArrayType THEN
  4766. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  4767. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  4768. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  4769. END;
  4770. ELSE
  4771. Error(position, "dimension on non math array type");
  4772. END;
  4773. (* ---- CAS ----- *)
  4774. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  4775. IF type0.IsComposite () THEN
  4776. Error(position, "first parameter of composite type");
  4777. result := SyntaxTree.invalidExpression;
  4778. ELSIF ~IsVariable (parameter0) THEN
  4779. Error(position, "first parameter not assignable");
  4780. result := SyntaxTree.invalidExpression;
  4781. ELSIF ~CompatibleTo(system,type1,type0) THEN
  4782. Error(position, "second parameter incompatible");
  4783. result := SyntaxTree.invalidExpression;
  4784. ELSIF ~CompatibleTo(system,type2,type0) THEN
  4785. Error(position, "third parameter incompatible");
  4786. result := SyntaxTree.invalidExpression;
  4787. ELSE
  4788. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  4789. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  4790. type := type0;
  4791. END;
  4792. (* ---- RESHAPE ----- *)
  4793. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  4794. IF type0 IS SyntaxTree.MathArrayType THEN
  4795. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4796. base := ArrayBase(type0,MAX(LONGINT));
  4797. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4798. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  4799. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4800. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4801. IF ~CompatibleTo(system,type0,parameterType) THEN
  4802. Error(parameter0.position,"incompatible parameter in reshape");
  4803. result := SyntaxTree.invalidExpression;
  4804. ELSE
  4805. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4806. END;
  4807. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4808. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4809. IF ~CompatibleTo(system,type1,parameterType) THEN
  4810. Error(parameter1.position,"parameter incompatible to math array of longint");
  4811. result := SyntaxTree.invalidExpression;
  4812. ELSE
  4813. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4814. END;
  4815. ELSE
  4816. Error(position,"reshape on non math array type");
  4817. result := SyntaxTree.invalidExpression;
  4818. END;
  4819. (* ---- SYSTEM.TYPECODE ----- *)
  4820. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  4821. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  4822. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  4823. type := type.resolved;
  4824. IF type IS SyntaxTree.PointerType THEN
  4825. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  4826. END;
  4827. IF ~(type IS SyntaxTree.RecordType) THEN
  4828. Error(parameter0.position,"must be type with type descriptor");
  4829. END;
  4830. ELSE
  4831. Error(parameter0.position,"is not a type symbol");
  4832. END;
  4833. type := system.addressType;
  4834. (* -------- FLT --------- *)
  4835. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  4836. type := system.realType;
  4837. IF IsRealValue(parameter0, r) THEN
  4838. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  4839. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  4840. value := ABS(i0);
  4841. IF value # 0 THEN
  4842. i1 := 23;
  4843. IF value >= 2*800000H THEN
  4844. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  4845. ELSIF value < 800000H THEN
  4846. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  4847. END;
  4848. value := (i1 + 127)*800000H - 800000H + value;
  4849. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  4850. END;
  4851. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value)));
  4852. END;
  4853. (* ------- CONNECT -------*)
  4854. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  4855. (*IF ~(currentIsCellNet) THEN
  4856. Error(position, "connection outside activeCells body block");
  4857. END;*)
  4858. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  4859. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  4860. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  4861. END;
  4862. IF numberActualParameters = 3 THEN
  4863. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  4864. Error(position, "incompatible channel size parameter");
  4865. END;
  4866. *)
  4867. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  4868. actualParameters.SetExpression(2,parameter2);
  4869. END;
  4870. activeCellsStatement := TRUE;
  4871. (* ---------- DELEGATE --------*)
  4872. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  4873. (*
  4874. IF ~(currentIsCellNet) THEN
  4875. Error(position, "connection delegation outside activeCells body block");
  4876. END;
  4877. *)
  4878. IF ~CheckPortType(parameter1, inPort) THEN
  4879. Error(parameter0.position,"not a port")
  4880. ELSIF ~CheckPortType(parameter0, outPort) THEN
  4881. Error(parameter1.position,"not a port")
  4882. ELSIF (outPort.direction # inPort.direction) THEN
  4883. Error(parameter0.position,"invalid port direction");
  4884. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  4885. Error(position, "incompatible port sizes");
  4886. END;
  4887. activeCellsStatement := TRUE;
  4888. (* --------- RECEIVE ---------*)
  4889. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  4890. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  4891. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  4892. IF inPort.direction # SyntaxTree.InPort THEN
  4893. Error(parameter0.position,"not an in-port")
  4894. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  4895. Error(parameter1.position,"incompatible to port type");
  4896. END;
  4897. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  4898. IF ~SameType(parameter2.type, system.integerType) THEN
  4899. Error(parameter2.position,"incompatible to integer type");
  4900. END;
  4901. END;
  4902. END;
  4903. (* --------- SEND ---------*)
  4904. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  4905. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  4906. IF CheckPortType(parameter0,outPort) THEN
  4907. IF outPort.direction # SyntaxTree.OutPort THEN
  4908. Error(parameter1.position,"not an out-port")
  4909. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  4910. Error(parameter1.position,"incompatible to port type");
  4911. ELSE
  4912. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  4913. actualParameters.SetExpression(1,parameter1);
  4914. END;
  4915. END;
  4916. (* ------- custom builtins ----- *)
  4917. ELSIF id = Global.systemSpecial THEN
  4918. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  4919. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  4920. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  4921. type := procedureType.returnType;
  4922. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  4923. (* go through all formal parameters *)
  4924. formalParameter := procedureType.firstParameter;
  4925. FOR i := 0 TO actualParameters.Length() - 1 DO
  4926. actualParameter := actualParameters.GetExpression(i);
  4927. IF actualParameter = SyntaxTree.invalidExpression THEN
  4928. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4929. Error(position, "incompatible parameter")
  4930. ELSE
  4931. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  4932. END;
  4933. actualParameters.SetExpression(i, actualParameter);
  4934. formalParameter := formalParameter.nextParameter
  4935. END
  4936. END
  4937. ELSE
  4938. Error(position, "builtin not implemented");
  4939. result := SyntaxTree.invalidExpression;
  4940. END;
  4941. END;
  4942. IF result # SyntaxTree.invalidExpression THEN
  4943. type := ResolveType(type);
  4944. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  4945. result.SetType(type);
  4946. END;
  4947. RETURN result
  4948. END NewBuiltinCallDesignator;
  4949. (** return type guard designator left(type)
  4950. - check if type can be extended (i.e. is no static record)
  4951. - check if type is a type extension of left.type
  4952. - returns new type guard designator
  4953. returns invalidDesignator = invalidExpression if error
  4954. **)
  4955. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  4956. VAR result: SyntaxTree.Designator;
  4957. BEGIN
  4958. result := SyntaxTree.invalidDesignator;
  4959. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  4960. Error(position, "no type extension of type");
  4961. IF VerboseErrorMessage THEN
  4962. Printout.Info("left",left);
  4963. Printout.Info("type",type);
  4964. END;
  4965. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  4966. Error(position, "variable cannot be extended");
  4967. ELSIF IsUnsafePointer(left.type) THEN
  4968. Error(position, "forbidden type guard on unsafe pointer");
  4969. ELSE
  4970. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  4971. result.SetType(type);
  4972. result.SetAssignable(left.assignable);
  4973. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  4974. END;
  4975. RETURN result
  4976. END NewTypeGuardDesignator;
  4977. (** check and resolve parameter designator left(expression list)
  4978. - check expression list
  4979. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  4980. - elsif left is a procedure type then
  4981. - if left is a built-in procedure then return NewBuiltinCallDesignator
  4982. - else return is a procedure call then return ProcedureCallDesignator
  4983. returns invalidDesignator = invalidExpression if error
  4984. **)
  4985. PROCEDURE ResolveParameterDesignator(designator: SyntaxTree.ParameterDesignator): SyntaxTree.Expression;
  4986. VAR
  4987. parameters: SyntaxTree.ExpressionList;
  4988. left: SyntaxTree.Designator;
  4989. result,expression: SyntaxTree.Expression;
  4990. typeDeclaration: SyntaxTree.TypeDeclaration;
  4991. type, expressionType: SyntaxTree.Type;
  4992. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  4993. BEGIN
  4994. type := type.resolved;
  4995. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  4996. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  4997. END;
  4998. RETURN type
  4999. END BaseType;
  5000. BEGIN
  5001. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5002. result := SyntaxTree.invalidDesignator;
  5003. left := ResolveDesignator(designator.left);
  5004. IF left # SyntaxTree.invalidDesignator THEN
  5005. parameters := designator.parameters;
  5006. IF ExpressionList(parameters) THEN
  5007. IF (left.type = NIL) THEN
  5008. Error(left.position,"object is not a procedure or cannot be extended");
  5009. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5010. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5011. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5012. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5013. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5014. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5015. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5016. ELSE
  5017. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5018. END
  5019. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5020. expression := parameters.GetExpression(0);
  5021. type := typeDeclaration.declaredType.resolved;
  5022. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5023. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5024. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5025. OR (expressionType IS SyntaxTree.EnumerationType)
  5026. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5027. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5028. Error(left.position,"invalid unsigned type in explicit conversion");
  5029. ELSE
  5030. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5031. END;
  5032. ELSE
  5033. Error(left.position,"invalid type in explicit conversion");
  5034. END;
  5035. ELSE
  5036. Error(left.position,"called object is not a procedure or cannot be extended");
  5037. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5038. result := SyntaxTree.invalidDesignator;
  5039. END;
  5040. ELSE
  5041. result := SyntaxTree.invalidDesignator
  5042. END;
  5043. END;
  5044. RETURN result;
  5045. END ResolveParameterDesignator;
  5046. (** check dereference designator left^
  5047. - check if left is pointer type or left is object type
  5048. - return new dereference designator with type = left.baseType.type (if appropriate)
  5049. with error handling
  5050. returns invalidDesignator = invalidExpression if error
  5051. **)
  5052. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5053. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5054. BEGIN
  5055. result := SyntaxTree.invalidDesignator;
  5056. type := left.type;
  5057. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5058. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5059. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5060. result.SetAssignable(TRUE);
  5061. result.SetType(type);
  5062. result.SetHidden(left.isHidden);
  5063. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5064. type := type.resolved;
  5065. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5066. result.SetAssignable(TRUE);
  5067. result.SetType(type);
  5068. result.SetHidden(left.isHidden);
  5069. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5070. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5071. result.SetAssignable(TRUE);
  5072. result.SetType(type);
  5073. result.SetHidden(left.isHidden);
  5074. ELSE
  5075. Error(position, "dereference on no pointer");
  5076. IF VerboseErrorMessage THEN
  5077. Printout.Info("pointer", type);
  5078. Printout.Info("scope", currentScope);
  5079. END;
  5080. END;
  5081. RETURN result
  5082. END NewDereferenceDesignator;
  5083. (** check supercall designator left^
  5084. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5085. - return new supercall designator with type = left.type
  5086. with error handling
  5087. **)
  5088. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5089. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5090. objectScope: SyntaxTree.Scope;
  5091. BEGIN
  5092. result := SyntaxTree.invalidDesignator;
  5093. IF left = SyntaxTree.invalidDesignator THEN
  5094. (* error already handled *)
  5095. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5096. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5097. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5098. IF symbol IS SyntaxTree.Procedure THEN
  5099. procedure := symbol(SyntaxTree.Procedure);
  5100. objectScope := currentScope;
  5101. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5102. objectScope := objectScope.outerScope;
  5103. END;
  5104. IF (left.left = NIL) OR ~
  5105. (
  5106. (left.left IS SyntaxTree.SelfDesignator) OR
  5107. (left.left IS SyntaxTree.DereferenceDesignator)
  5108. & (left.left(SyntaxTree.Designator).left # NIL)
  5109. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5110. Error(position, "procedure not in immediate object scope");
  5111. IF VerboseErrorMessage THEN
  5112. Printout.Info("left.left",left.left);
  5113. END;
  5114. ELSIF procedure.super # NIL THEN
  5115. result := SyntaxTree.NewSupercallDesignator(position,left);
  5116. result.SetType(left.type.resolved)
  5117. ELSE
  5118. Error(position, "no supermethod for this procedure");
  5119. END;
  5120. ELSE
  5121. Error(position, "symbol is not a procedure");
  5122. END;
  5123. ELSE
  5124. Error(position, "is no symbol designator");
  5125. END;
  5126. RETURN result
  5127. END NewSupercallDesignator;
  5128. (** check and semantically resolve arrow designator left^
  5129. - if left is procedure type -> result := SupercallDesignator
  5130. - else result := DereferenceDesignator
  5131. returns result via global variable resolvedExpression
  5132. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5133. **)
  5134. PROCEDURE ResolveArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator): SyntaxTree.Expression;
  5135. VAR left: SyntaxTree.Designator;
  5136. BEGIN
  5137. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5138. left := ResolveDesignator(arrowDesignator.left);
  5139. IF left # NIL THEN
  5140. IF (left.type = NIL) THEN
  5141. Error(arrowDesignator.position,"Invalid arrow designator");
  5142. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5143. RETURN NewSupercallDesignator(arrowDesignator.position,left);
  5144. ELSE
  5145. IF IsPointerToObject(left.type) THEN
  5146. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  5147. END;
  5148. RETURN NewDereferenceDesignator(arrowDesignator.position,left)
  5149. END
  5150. END;
  5151. RETURN arrowDesignator;
  5152. END ResolveArrowDesignator;
  5153. (** check and return expression
  5154. - if expression has no type then resolve expression
  5155. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5156. - return result
  5157. **)
  5158. PROCEDURE ResolveExpression*(x: SyntaxTree.Expression): SyntaxTree.Expression;
  5159. VAR result: SyntaxTree.Expression; subtype: LONGINT; type: SyntaxTree.Type;
  5160. BEGIN
  5161. IF (x = NIL) OR (x = SyntaxTree.invalidExpression) OR (x.type # NIL) THEN RETURN x END;
  5162. IF ~(x IS SyntaxTree.BuiltinCallDesignator) THEN
  5163. x.SetType(SyntaxTree.invalidType);
  5164. END;
  5165. result := x;
  5166. WITH x:
  5167. SyntaxTree.ResultDesignator DO result := ResolveResultDesignator(x)
  5168. | SyntaxTree.SelfDesignator DO result := ResolveSelfDesignator(x)
  5169. | SyntaxTree.TypeGuardDesignator DO result := x;
  5170. | SyntaxTree.SymbolDesignator DO result := x;
  5171. | SyntaxTree.BuiltinCallDesignator DO result := ResolveBuiltinCallDesignator(x)
  5172. | SyntaxTree.ProcedureCallDesignator DO
  5173. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  5174. | SyntaxTree.BracketDesignator DO result := ResolveBracketDesignator(x)
  5175. | SyntaxTree.ArrowDesignator DO result := ResolveArrowDesignator(x)
  5176. | SyntaxTree.ParameterDesignator DO result := ResolveParameterDesignator(x)
  5177. | SyntaxTree.SelectorDesignator DO result := ResolveSelectorDesignator(x)
  5178. | SyntaxTree.IdentifierDesignator DO result := ResolveIdentifierDesignator(x)
  5179. | SyntaxTree.TensorRangeExpression DO x.SetType(NIL);
  5180. | SyntaxTree.RangeExpression DO result := ResolveRangeExpression(x)
  5181. | SyntaxTree.BinaryExpression DO result := ResolveBinaryExpression(x)
  5182. | SyntaxTree.UnaryExpression DO result := ResolveUnaryExpression(x)
  5183. | SyntaxTree.MathArrayExpression DO result := ResolveMathArrayExpression(x)
  5184. | SyntaxTree.Set DO result := ResolveSet(x)
  5185. | SyntaxTree.BooleanValue DO x.SetType(system.booleanType);
  5186. | SyntaxTree.IntegerValue DO x.SetType(Global.GetIntegerType(system,x.value));
  5187. | SyntaxTree.CharacterValue DO x.SetType(system.characterType);
  5188. | SyntaxTree.SetValue DO
  5189. x.SetType(system.setType);
  5190. | SyntaxTree.MathArrayValue DO
  5191. x.SetType(SyntaxTree.invalidType)
  5192. | SyntaxTree.RealValue DO
  5193. subtype := x.subtype;
  5194. IF subtype = Scanner.Real THEN
  5195. type := system.realType
  5196. ELSIF subtype = Scanner.Longreal THEN
  5197. type := system.longrealType
  5198. ELSE
  5199. HALT(100)
  5200. END;
  5201. x.SetType(type);
  5202. | SyntaxTree.ComplexValue DO
  5203. subtype := x.subtype;
  5204. IF subtype = Scanner.Real THEN
  5205. type := system.complexType
  5206. ELSIF subtype = Scanner.Longreal THEN
  5207. type := system.longcomplexType
  5208. ELSE
  5209. HALT(100)
  5210. END;
  5211. x.SetType(type);
  5212. | SyntaxTree.StringValue DO
  5213. x.SetType(ResolveType(SyntaxTree.NewStringType(x.position,system.characterType,x.length)));
  5214. | SyntaxTree.NilValue DO
  5215. x.SetType(system.nilType);
  5216. | SyntaxTree.EnumerationValue DO
  5217. x.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  5218. END; (* no other case allowed *)
  5219. IF currentIsRealtime THEN
  5220. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5221. Error(x.position,"forbidden non-realtime expression in realtime procedure");
  5222. END;
  5223. END;
  5224. (* designator modifiers for backends if they support it ...*)
  5225. WITH x: SyntaxTree.Designator DO
  5226. IF (x.modifiers # NIL) THEN
  5227. WITH result: SyntaxTree.Designator DO
  5228. result.SetModifiers(x.modifiers);
  5229. CheckModifiers(result.modifiers, FALSE);
  5230. ELSE (* error case *)
  5231. END;
  5232. END;
  5233. ELSE
  5234. END;
  5235. RETURN result;
  5236. END ResolveExpression;
  5237. (**
  5238. check expression to be constant expression
  5239. - resolve expression
  5240. - if valid then check that of value type
  5241. report error and return invalidExpression if anything fails
  5242. **)
  5243. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5244. VAR position: Position;
  5245. BEGIN
  5246. position := expression.position;
  5247. expression := ResolveExpression(expression);
  5248. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5249. ELSIF (expression.resolved = NIL) THEN
  5250. Error(position, "expression is not constant");
  5251. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5252. expression := SyntaxTree.invalidExpression;
  5253. END;
  5254. RETURN expression
  5255. END ConstantExpression;
  5256. (** check expression to be constant integer
  5257. - resolve expresssion
  5258. - if valid then check that of integer value type
  5259. report error and return invalidExpression if anything fails
  5260. **)
  5261. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5262. VAR position: Position;
  5263. BEGIN
  5264. position := expression.position;
  5265. expression := ResolveExpression(expression);
  5266. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5267. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5268. expression := SyntaxTree.invalidExpression;
  5269. Error(position, "expression is not a constant integer");
  5270. END;
  5271. RETURN expression
  5272. END ConstantInteger;
  5273. (** check expression as positive (>=0) constant integer
  5274. - resolve expression
  5275. - if valid then check that integer value
  5276. - if integer value then check that value >= 0
  5277. report error and return invalidExpression if anything fails
  5278. **)
  5279. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5280. VAR position: Position;
  5281. BEGIN
  5282. position := expression.position;
  5283. expression := ConstantExpression(expression);
  5284. IF expression = SyntaxTree.invalidExpression THEN
  5285. (* error already reported *)
  5286. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5287. Error(position, "expression is not integer valued");
  5288. expression := SyntaxTree.invalidExpression
  5289. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value <0) THEN
  5290. Error(position, "integer is not greater or equal zero");
  5291. END;
  5292. RETURN expression
  5293. END ConstantIntegerGeq0;
  5294. (** check expression as condition
  5295. - resolve expression
  5296. - if valid expression then check that result type is boolean
  5297. report error and return invalidExpression if anything fails
  5298. **)
  5299. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5300. VAR position: Position;
  5301. BEGIN
  5302. position := expression.position;
  5303. expression := ResolveExpression(expression);
  5304. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5305. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5306. expression := SyntaxTree.invalidExpression;
  5307. Error(position, "expression is not boolean");
  5308. END;
  5309. RETURN expression
  5310. END ResolveCondition;
  5311. (*** symbols ***)
  5312. PROCEDURE ResolveSymbol*(x: SyntaxTree.Symbol);
  5313. BEGIN
  5314. WITH
  5315. x:
  5316. | SyntaxTree.TypeDeclaration DO ResolveTypeDeclaration(x)
  5317. | SyntaxTree.Constant DO ResolveConstant(x)
  5318. | SyntaxTree.Parameter DO ResolveParameter(x)
  5319. | SyntaxTree.Variable DO ResolveVariable(x)
  5320. | SyntaxTree.Operator DO ResolveOperator(x)
  5321. | SyntaxTree.Procedure DO ResolveProcedure(x)
  5322. | SyntaxTree.Builtin DO ResolveBuiltin(x)
  5323. | SyntaxTree.Import DO ResolveImport(x)
  5324. END;
  5325. END ResolveSymbol;
  5326. (** check a symbol
  5327. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5328. **)
  5329. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5330. VAR scope: SyntaxTree.Scope;
  5331. BEGIN
  5332. (* visibility *)
  5333. scope := symbol.scope;
  5334. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5335. scope := scope.outerScope;
  5336. END;
  5337. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5338. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5339. Error(symbol.position,"cannot be exported");
  5340. IF VerboseErrorMessage THEN
  5341. Printout.Info("symbol",symbol);
  5342. END;
  5343. END;
  5344. END;
  5345. END CheckSymbolVisibility;
  5346. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5347. If node is currently being resolved then emit a cyclic definition error.
  5348. Return TRUE only if node is fully resolved.
  5349. **)
  5350. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5351. VAR result: BOOLEAN;
  5352. BEGIN
  5353. IF SyntaxTree.Resolved IN x.state THEN
  5354. result := FALSE
  5355. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5356. Error(x.position,"cyclic definition");
  5357. result := FALSE;
  5358. ELSE
  5359. result := TRUE;
  5360. x.SetState(SyntaxTree.BeingResolved)
  5361. END;
  5362. RETURN result
  5363. END SymbolNeedsResolution;
  5364. (** check and resolve a type declaration symbol = Type
  5365. - set type to declaration type
  5366. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  5367. This is so because the type declaration itself does not have a type but it only stands for a type.
  5368. In the implementation of the compiler this made a lot much easier.
  5369. - resolve and set declared type
  5370. - check symbol
  5371. **)
  5372. PROCEDURE ResolveTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  5373. VAR prevScope: SyntaxTree.Scope;
  5374. BEGIN
  5375. IF Trace THEN D.Str("ResolveTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  5376. IF SymbolNeedsResolution(typeDeclaration) THEN
  5377. typeDeclaration.SetState(SyntaxTree.Resolved);
  5378. prevScope := currentScope;
  5379. currentScope := typeDeclaration.scope;
  5380. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  5381. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  5382. CheckSymbolVisibility(typeDeclaration);
  5383. typeDeclaration.SetState(SyntaxTree.Resolved);
  5384. currentScope := prevScope;
  5385. END;
  5386. END ResolveTypeDeclaration;
  5387. (** check and resolve a constant declaration symbol = (constant) expression
  5388. - check expression
  5389. - set type and value
  5390. - check symbol
  5391. **)
  5392. PROCEDURE ResolveConstant(constant: SyntaxTree.Constant);
  5393. VAR
  5394. expression: SyntaxTree.Expression;
  5395. type: SyntaxTree.Type;
  5396. name: Basic.SegmentedName;
  5397. replacement: Replacement;
  5398. BEGIN
  5399. IF Trace THEN D.Str("ResolveConstant "); D.Str0(constant.name); D.Ln; END;
  5400. IF SymbolNeedsResolution(constant) THEN
  5401. expression := constant.value;
  5402. IF replacements # NIL THEN
  5403. Global.GetSymbolSegmentedName(constant, name);
  5404. replacement := replacements;
  5405. WHILE (replacement # NIL) & (replacement.name # name) DO
  5406. replacement := replacement.next;
  5407. END;
  5408. IF replacement # NIL THEN
  5409. InfoSS(constant.position, "replacing constant", constant.name);
  5410. (*
  5411. NEW(stringReader, Strings.Length(replacement.string^));
  5412. stringReader.Set(replacement.string^);
  5413. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  5414. NEW(parser, scanner, diagnostics);
  5415. expression := parser.Expression();
  5416. *)
  5417. expression := replacement.expression;
  5418. replacement.used := TRUE;
  5419. END;
  5420. END;
  5421. constant.SetType(SyntaxTree.invalidType);
  5422. expression := ConstantExpression(expression);
  5423. ASSERT(expression.type # NIL);
  5424. type := expression.type.resolved;
  5425. constant.SetType(type);
  5426. constant.SetValue(expression);
  5427. CheckSymbolVisibility(constant);
  5428. constant.SetState(SyntaxTree.Resolved);
  5429. END;
  5430. END ResolveConstant;
  5431. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  5432. VAR procedureAlignment: LONGINT;
  5433. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  5434. (* least common multiple *)
  5435. VAR a,b: LONGINT;
  5436. BEGIN
  5437. a := a0; b := b0;
  5438. WHILE (a # b) DO
  5439. IF a < b THEN a := a+a0
  5440. ELSE b := b + b0
  5441. END;
  5442. END;
  5443. RETURN a
  5444. END LCM;
  5445. BEGIN
  5446. IF alignment > 1 THEN
  5447. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  5448. IF (procedureAlignment > 1) THEN
  5449. alignment := LCM(alignment, procedureAlignment);
  5450. END;
  5451. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  5452. END;
  5453. END AdaptStackAlignment;
  5454. (** check and resolve a variable / field
  5455. - check and set type
  5456. - negative check on open array type
  5457. - check symbol
  5458. **)
  5459. PROCEDURE ResolveVariable(variable: SyntaxTree.Variable);
  5460. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position; pointerType: SyntaxTree.PointerType;
  5461. BEGIN
  5462. IF Trace THEN D.Str("ResolveVariable "); D.Str0(variable.name); D.Ln; END;
  5463. IF SymbolNeedsResolution(variable) THEN
  5464. modifiers := variable.modifiers;
  5465. (*
  5466. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  5467. variable.AddFlags(flags);
  5468. *)
  5469. variable.SetType(ResolveType(variable.type));
  5470. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  5471. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  5472. Error(variable.position,"forbidden open array variable");
  5473. END;
  5474. END;
  5475. CheckSymbolVisibility(variable);
  5476. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  5477. variable.SetUntraced(TRUE);
  5478. IF ~ContainsPointer(variable.type) THEN
  5479. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  5480. Error(position, "untraced flag on non-pointer variable");
  5481. END;
  5482. END;
  5483. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  5484. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  5485. IF ~PowerOf2(value) THEN
  5486. Error(position, "forbidden alignment - must be power of two");
  5487. ELSE
  5488. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, LONGINT (value)); (* TODO: fix explicit integer truncation *)
  5489. END;
  5490. END;
  5491. variable.SetAlignment(FALSE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  5492. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  5493. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  5494. Error(position, "fixed position not possible in procedure");
  5495. END;
  5496. variable.SetAlignment(TRUE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  5497. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  5498. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  5499. Error(position,"fictive offset not possible in procedure");
  5500. END;
  5501. variable.SetFictive(LONGINT (value)); (* TODO: fix explicit integer truncation *)
  5502. variable.SetOffset(LONGINT(value)*system.dataUnit); (* TODO: fix explicit integer truncation *)
  5503. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  5504. END;
  5505. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  5506. IF variable.type.resolved IS SyntaxTree.CellType THEN
  5507. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  5508. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  5509. END;
  5510. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  5511. IF variable.initializer # NIL THEN
  5512. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  5513. END;
  5514. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  5515. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  5516. pointerType.SetPointerBase(variable.type);
  5517. pointerType.SetHidden(TRUE);
  5518. variable.SetType(ResolveType(pointerType));
  5519. END;
  5520. variable.SetState(SyntaxTree.Resolved);
  5521. END;
  5522. END ResolveVariable;
  5523. (** check and resolve a (procedure) parameter
  5524. - check and set type
  5525. - check symbol
  5526. - check parameter kind and set read-only flags if appropriate
  5527. **)
  5528. PROCEDURE ResolveParameter(parameter: SyntaxTree.Parameter);
  5529. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  5530. BEGIN
  5531. IF Trace THEN D.Str("ResolveParameter "); D.Str0(parameter.name); D.Ln; END;
  5532. IF SymbolNeedsResolution(parameter) THEN
  5533. modifiers := parameter.modifiers;
  5534. parameter.SetType(ResolveType(parameter.type));
  5535. ASSERT(parameter.type.resolved # NIL);
  5536. CheckSymbolVisibility(parameter);
  5537. IF parameter.defaultValue # NIL THEN
  5538. IF parameter.kind # SyntaxTree.ValueParameter THEN
  5539. Error(parameter.position,"forbidden default value on non-value parameter");
  5540. ELSE
  5541. expression := ConstantExpression(parameter.defaultValue);
  5542. IF CompatibleTo(system,expression.type, parameter.type) THEN
  5543. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  5544. parameter.SetDefaultValue(expression);
  5545. END;
  5546. END;
  5547. END;
  5548. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  5549. Error(parameter.position, "forbidden value parameter of math array type ");
  5550. END;
  5551. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  5552. parameter.SetUntraced(TRUE);
  5553. IF ~ContainsPointer(parameter.type) THEN
  5554. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  5555. Error(position, "untraced flag on non-pointer variable");
  5556. END;
  5557. END;
  5558. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  5559. parameter.SetMoveable(TRUE);
  5560. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  5561. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  5562. Error(position, "illegal movable flag on non-address variable");
  5563. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  5564. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  5565. Error(position, "unnecessary movable flag on variable variable");
  5566. END;
  5567. END;
  5568. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  5569. parameter.SetState(SyntaxTree.Resolved);
  5570. END;
  5571. END ResolveParameter;
  5572. (** check and resolve a procedure (with declaration and implementation scope)
  5573. - check the procedure type
  5574. - check if method (i.e. in record scope), if so then
  5575. - check if (unique) constructor
  5576. - check if (unique) finalizer
  5577. - check if super method available, if so then check signature
  5578. - of not in record scope then negative check on constructor flag
  5579. - of not in record scope then negative check on finalizer flag
  5580. - check declarations (including a delayed implementation check, cf procedure Declarations)
  5581. - check procedure symbol
  5582. **)
  5583. PROCEDURE ResolveProcedure(procedure: SyntaxTree.Procedure);
  5584. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  5585. procedureType: SyntaxTree.ProcedureType;
  5586. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  5587. qualifiedType: SyntaxTree.QualifiedType;
  5588. value: Basic.Integer;
  5589. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  5590. position: Position;
  5591. fp: SyntaxTree.FingerPrint;
  5592. BEGIN
  5593. IF Trace THEN D.Str("ResolveProcedure "); D.Str0(procedure.name); D.Ln; END;
  5594. IF IsOberonInline(procedure) THEN
  5595. IF SyntaxTree.Public * procedure.access # {} THEN
  5596. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  5597. END;
  5598. procedure.SetInline(FALSE);
  5599. procedure.SetOberonInline(TRUE);
  5600. END;
  5601. IF SymbolNeedsResolution(procedure) THEN
  5602. recentIsRealtime := currentIsRealtime;
  5603. recentIsBodyProcedure := currentIsBodyProcedure;
  5604. CheckSymbolVisibility(procedure);
  5605. IF Trace THEN D.Str("undefined"); D.Ln; END;
  5606. procedureType := procedure.type(SyntaxTree.ProcedureType);
  5607. modifiers := procedureType.modifiers;
  5608. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN
  5609. procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  5610. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  5611. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  5612. END;
  5613. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  5614. procedureType.SetInterrupt(TRUE);
  5615. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  5616. END;
  5617. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  5618. procedureType.SetNoReturn(TRUE);
  5619. END;
  5620. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  5621. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  5622. IF (procedure.scope IS SyntaxTree.ModuleScope) THEN
  5623. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE);
  5624. ELSIF HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE);
  5625. END;
  5626. END;
  5627. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  5628. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  5629. END;
  5630. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  5631. IF ~PowerOf2(value) THEN
  5632. Error(position, "forbidden stack alignment - must be power of two");
  5633. ELSE
  5634. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  5635. END;
  5636. END;
  5637. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  5638. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  5639. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  5640. END;
  5641. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  5642. SyntaxTree.InitFingerPrint(fp);
  5643. fp.shallow := LONGINT(value); (* TODO: fix explicit integer truncation *)
  5644. fp.public := LONGINT(value); (* TODO: fix explicit integer truncation *)
  5645. fp.private := LONGINT(value); (* TODO: fix explicit integer truncation *)
  5646. fp.shallowAvailable := TRUE;
  5647. procedure.SetFingerPrint(fp);
  5648. END;
  5649. CheckModifiers(modifiers, TRUE);
  5650. modifiers := procedureType.returnTypeModifiers;
  5651. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  5652. CheckModifiers(modifiers, TRUE);
  5653. procedure.SetState(SyntaxTree.Resolved);
  5654. FixProcedureType(procedureType);
  5655. currentIsRealtime := procedureType.isRealtime;
  5656. currentIsBodyProcedure := procedure.isBodyProcedure;
  5657. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  5658. THEN
  5659. Error(procedure.position,"problems during parameter offset computation");
  5660. END;
  5661. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  5662. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  5663. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  5664. Error(procedure.position,"problem during parameter offset generation");
  5665. END;
  5666. END;
  5667. IF procedure.scope IS SyntaxTree.RecordScope THEN
  5668. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  5669. procedureType.SetDelegate(TRUE);
  5670. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  5671. (* add auto-self *)
  5672. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  5673. IF (record.pointerType.typeDeclaration = NIL) THEN
  5674. selfParameter.SetType(record.pointerType);
  5675. ELSE
  5676. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  5677. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  5678. qualifiedType.SetResolved(record.pointerType);
  5679. selfParameter.SetType(qualifiedType);
  5680. END;
  5681. selfParameter.SetAccess(SyntaxTree.Hidden);
  5682. ELSIF (procedureType.selfParameter = NIL) THEN
  5683. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.VarParameter);
  5684. IF (record.typeDeclaration = NIL) THEN
  5685. selfParameter.SetType(record);
  5686. ELSE
  5687. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.typeDeclaration.name);
  5688. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  5689. qualifiedType.SetResolved(record);
  5690. selfParameter.SetType(qualifiedType);
  5691. END;
  5692. procedureType.SetSelfParameter(selfParameter);
  5693. END;
  5694. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  5695. IF procedure.isConstructor THEN
  5696. (*! constructor is always visible, compatibility to paco
  5697. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  5698. *)
  5699. procedure.MarkUsed;
  5700. IF procedureType.returnType # NIL THEN
  5701. Error(procedure.position,"constructor with forbidden return type");
  5702. END;
  5703. proc := procedure.scope.firstProcedure;
  5704. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  5705. proc := proc.nextProcedure;
  5706. END;
  5707. IF proc # NIL THEN
  5708. Error(procedure.position,"duplicate constructor")
  5709. ELSE
  5710. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  5711. END;
  5712. END;
  5713. IF procedure.isFinalizer THEN
  5714. procedure.MarkUsed;
  5715. IF procedureType.returnType # NIL THEN
  5716. Error(procedure.position,"finalizer with forbidden return type");
  5717. END;
  5718. IF procedureType.numberParameters # 0 THEN
  5719. Error(procedure.position,"finalizer with formal parameters");
  5720. END;
  5721. proc := procedure.scope.firstProcedure;
  5722. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  5723. proc := proc.nextProcedure;
  5724. END;
  5725. IF proc # NIL THEN
  5726. Error(procedure.position,"duplicate finalizer")
  5727. ELSE
  5728. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  5729. END;
  5730. END;
  5731. super := FindSuperProcedure(record.recordScope, procedure);
  5732. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  5733. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  5734. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  5735. END;
  5736. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  5737. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  5738. END;
  5739. IF super.isFinal THEN
  5740. Error(procedure.position,"forbidden method extending final method");
  5741. END;
  5742. (*
  5743. IF super.access # procedure.access THEN
  5744. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  5745. END;
  5746. *)
  5747. procedure.SetSuper(super);
  5748. super.SetOverwritten(TRUE);
  5749. procedure.SetAccess(procedure.access+super.access);
  5750. procedure.MarkUsed;
  5751. END;
  5752. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  5753. THEN
  5754. Error(procedure.position,"problems during parameter offset computation");
  5755. END;
  5756. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  5757. IF cellsAreObjects THEN
  5758. procedureType.SetDelegate(TRUE);
  5759. END;
  5760. IF procedure.isConstructor THEN
  5761. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  5762. END;
  5763. ELSIF procedure.isConstructor THEN
  5764. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  5765. END;
  5766. Declarations(procedure.procedureScope, FALSE, {0,1});
  5767. (* body resolution part done as late fix of the procedure type *)
  5768. procedure.SetState(SyntaxTree.Resolved);
  5769. currentIsRealtime := recentIsRealtime;
  5770. currentIsBodyProcedure := recentIsBodyProcedure;
  5771. END;
  5772. END ResolveProcedure;
  5773. (**
  5774. a builtin procedure is a global item that may not be modified locally
  5775. instead the resolving of builtin procedure calls are done in the esignator
  5776. **)
  5777. PROCEDURE ResolveBuiltin(builtinProcedure: SyntaxTree.Builtin);
  5778. VAR type: SyntaxTree.Type;
  5779. BEGIN
  5780. type := ResolveType(builtinProcedure.type);
  5781. END ResolveBuiltin;
  5782. (* nopov *)
  5783. (** check and resolve operator
  5784. - operators are first checked as procedures
  5785. - then additional operator-specific checks are done
  5786. - note that only module-scope operators are checked here
  5787. (operators in a record scope are only allowed in the context of
  5788. array-structured object types and checked in 'ResolveArrayStructure')
  5789. - also note that inter-operator conformity is not checked here
  5790. **)
  5791. PROCEDURE ResolveOperator(operator: SyntaxTree.Operator);
  5792. VAR
  5793. procedureType: SyntaxTree.ProcedureType;
  5794. leftType, rightType: SyntaxTree.Type;
  5795. identifierNumber: LONGINT; position: Position;
  5796. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  5797. modifiers: SyntaxTree.Modifier;
  5798. (** whether a type is locally defined in the current module scope
  5799. for arrays, the base type must be locally defined **)
  5800. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  5801. BEGIN
  5802. IF type = NIL THEN
  5803. RETURN FALSE
  5804. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  5805. RETURN TRUE
  5806. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  5807. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  5808. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  5809. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  5810. ELSE
  5811. RETURN FALSE
  5812. END
  5813. END IsLocallyDefined;
  5814. BEGIN
  5815. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  5816. procedureType := operator.type(SyntaxTree.ProcedureType);
  5817. modifiers := procedureType.modifiers;
  5818. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  5819. CheckModifiers(modifiers, TRUE);
  5820. ResolveProcedure(operator);
  5821. IF operator.scope IS SyntaxTree.RecordScope THEN
  5822. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  5823. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  5824. IF identifierNumber = -1 THEN
  5825. Error(operator.position, "operator with unknown identifier")
  5826. ELSIF ~system.operatorDefined[identifierNumber] THEN
  5827. Error(operator.position, "identifier may not be used for operator")
  5828. ELSE
  5829. IF procedureType.numberParameters < 1 THEN
  5830. Error(operator.position, "operator without operand");
  5831. ELSIF procedureType.numberParameters > 2 THEN
  5832. Error(operator.position, "operator with more than two operands");
  5833. ELSE
  5834. (* determine operand types *)
  5835. leftType := procedureType.firstParameter.type;
  5836. IF procedureType.numberParameters > 1 THEN
  5837. rightType := procedureType.firstParameter.nextParameter.type
  5838. ELSE
  5839. rightType := NIL
  5840. END;
  5841. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  5842. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  5843. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  5844. Error(operator.position, "none of the operands is declared in the same module")
  5845. END
  5846. END;
  5847. (* TODO: refine the checks, think about how restrictive the checks should be
  5848. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  5849. They might be used for intersection, union, complement of custom object types *)
  5850. (* defaults *)
  5851. hasReturnType := TRUE;
  5852. mustBeUnary := FALSE;
  5853. mustBeBinary := FALSE;
  5854. mustReturnBoolean := FALSE;
  5855. mustReturnInteger := FALSE;
  5856. mustHaveEquitypedOperands := FALSE;
  5857. (* operator-specific exceptions *)
  5858. CASE identifierNumber OF
  5859. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  5860. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  5861. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  5862. mustBeBinary := TRUE
  5863. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  5864. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  5865. | Scanner.Times: mustBeBinary := TRUE
  5866. | Scanner.TimesTimes: mustBeBinary := TRUE
  5867. | Scanner.DotTimes: mustBeBinary := TRUE
  5868. | Scanner.PlusTimes: mustBeBinary := TRUE
  5869. | Scanner.Slash: mustBeBinary := TRUE
  5870. | Scanner.Backslash: mustBeBinary := TRUE
  5871. | Scanner.DotSlash: mustBeBinary := TRUE
  5872. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  5873. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  5874. | Scanner.Not: mustBeUnary := TRUE
  5875. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  5876. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  5877. | Scanner.Transpose: mustBeUnary := TRUE;
  5878. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  5879. | Global.DotTimesPlus: mustBeBinary := TRUE;
  5880. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  5881. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  5882. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  5883. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  5884. | Global.Abs: mustBeUnary := TRUE;
  5885. | Global.Ash: (* TODO: arity? *)
  5886. | Global.Cap: (* TODO: arity? *)
  5887. | Global.Chr: mustBeUnary := TRUE;
  5888. | Global.Entier: (* TODO: arity? *)
  5889. | Global.EntierH: (* TODO: arity? *)
  5890. | Global.Len: (* unary and binary *)
  5891. | Global.Short, Global.Long: mustBeUnary := TRUE;
  5892. | Global.Max, Global.Min: (* unary and binary *)
  5893. | Global.Odd: (* TODO: arity? *)
  5894. | Global.Sum: (* TODO: arity? *)
  5895. | Global.All: (* TODO: arity? *)
  5896. | Global.Re, Global.Im:
  5897. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  5898. | Scanner.Alias:
  5899. | Scanner.GreaterGreater, Scanner.LessLess:
  5900. mustBeBinary := TRUE; hasReturnType := FALSE;
  5901. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  5902. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  5903. END;
  5904. (* check parameter count *)
  5905. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  5906. Error(operator.position,"operator is not unary")
  5907. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  5908. Error(operator.position,"operator is not binary")
  5909. END;
  5910. (* check parameter types *)
  5911. (* TODO: is this used at all? *)
  5912. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  5913. leftType := procedureType.firstParameter.type;
  5914. rightType := procedureType.firstParameter.nextParameter.type;
  5915. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  5916. Error(operator.position, "the two operands are not of the same type")
  5917. END
  5918. END;
  5919. (* check return type *)
  5920. IF hasReturnType THEN
  5921. IF procedureType.returnType = NIL THEN
  5922. Error(operator.position, "return type required")
  5923. ELSIF mustReturnBoolean THEN
  5924. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  5925. Error(operator.position,"return type is not Boolean")
  5926. END
  5927. ELSIF mustReturnInteger THEN
  5928. IF ~IsSizeType(procedureType.returnType, system.addressSize) THEN
  5929. Error(operator.position,"return type is no size type")
  5930. END
  5931. END
  5932. ELSIF procedureType.returnType # NIL THEN
  5933. Error(operator.position, "return type not allowed")
  5934. END
  5935. END
  5936. END
  5937. END
  5938. END ResolveOperator;
  5939. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  5940. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  5941. BEGIN
  5942. IF error THEN RETURN FALSE END;
  5943. prevScope := currentScope;
  5944. prevDiagnostics := diagnostics;
  5945. diagnostics := NIL; (* suppress error output *)
  5946. currentScope := module.moduleScope;
  5947. ResolveImport(x);
  5948. IF ~error THEN
  5949. module.moduleScope.AddImport(x);
  5950. x.SetScope(module.moduleScope);
  5951. END;
  5952. currentScope := prevScope;
  5953. diagnostics := prevDiagnostics;
  5954. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  5955. END AddImport;
  5956. (** check and resolve import
  5957. - check for name = SYSTEM
  5958. - check for forbidden self import
  5959. - search through global import cache: already imported?
  5960. - check if already imported indirectly
  5961. - import if necessary -> set module and enter into import cache
  5962. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  5963. - after this import this direct import and all indirect imports are stored in the current module's import list
  5964. **)
  5965. PROCEDURE ResolveImport(x: SyntaxTree.Import);
  5966. VAR
  5967. module: SyntaxTree.Module;
  5968. moduleScope: SyntaxTree.ModuleScope;
  5969. import,reimport: SyntaxTree.Import;
  5970. filename: FileName;
  5971. prevScope: SyntaxTree.Scope;
  5972. BEGIN
  5973. IF SymbolNeedsResolution(x) THEN
  5974. prevScope := currentScope;
  5975. x.SetType(SyntaxTree.importType);
  5976. moduleScope := currentScope.ownerModule.moduleScope;
  5977. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  5978. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  5979. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  5980. Error(x.position,"forbidden self import");
  5981. ELSE
  5982. (* search through global import list: already imported ? *)
  5983. IF (x.module = NIL) & (importCache # NIL) THEN
  5984. import := importCache.ImportByModuleName(x.moduleName,x.context);
  5985. ELSE import := NIL
  5986. END;
  5987. IF x.module # NIL THEN (* already imported indirectly *)
  5988. module := x.module;
  5989. ELSIF import # NIL THEN (* already in module list *)
  5990. module := import.module;
  5991. ASSERT(module # NIL);
  5992. x.SetModule(module);
  5993. ELSE (* must be imported *)
  5994. Global.ModuleFileName(x.moduleName,x.context,filename);
  5995. IF symbolFileFormat # NIL THEN
  5996. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  5997. IF module = NIL THEN
  5998. ErrorSS(x.position,"could not import",filename);
  5999. IF VerboseErrorMessage THEN
  6000. Printout.Info("import",x)
  6001. END
  6002. ELSE
  6003. (*
  6004. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6005. (*! should rather be done by importer *)
  6006. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6007. checker.importCache := importCache;
  6008. checker.arrayBaseImported := arrayBaseImported;
  6009. checker.global := global;
  6010. checker.Module(module); (* semantic check *)
  6011. error := error OR checker.error;
  6012. END;
  6013. *)
  6014. (*
  6015. ASSERT(SyntaxTree.Resolved IN module.state);
  6016. *)
  6017. x.SetModule(module);
  6018. IF importCache # NIL THEN
  6019. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6020. import.SetContext(x.context);
  6021. import.SetModule(module);
  6022. importCache.AddImport(import);
  6023. END;
  6024. END;
  6025. ELSE
  6026. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6027. END;
  6028. END;
  6029. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6030. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6031. END;
  6032. import := module.moduleScope.firstImport;
  6033. WHILE(import # NIL) DO
  6034. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6035. ASSERT(currentScope # NIL);
  6036. ASSERT(currentScope.ownerModule # NIL);
  6037. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6038. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6039. Error(x.position,"recursive import");
  6040. ELSE
  6041. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6042. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6043. IF reimport = NIL THEN (* indirect import *)
  6044. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6045. reimport.SetContext(import.context);
  6046. reimport.SetModule(import.module);
  6047. moduleScope.AddImport(reimport);
  6048. reimport.SetScope(moduleScope);
  6049. ELSE
  6050. ASSERT(import.module # NIL);
  6051. reimport.SetModule(import.module); (* direct or indirect import *)
  6052. END;
  6053. END;
  6054. import := import.nextImport;
  6055. END;
  6056. END;
  6057. END;
  6058. currentScope := prevScope;
  6059. (* ELSE nothing to be done *)
  6060. x.SetState(SyntaxTree.Resolved);
  6061. END;
  6062. END ResolveImport;
  6063. (*** statements ***)
  6064. PROCEDURE VStatement*(x: SyntaxTree.Statement);
  6065. BEGIN
  6066. WITH x:
  6067. SyntaxTree.ProcedureCallStatement DO VisitProcedureCallStatement(x)
  6068. | SyntaxTree.Assignment DO VisitAssignment(x)
  6069. | SyntaxTree.CommunicationStatement DO VisitCommunicationStatement(x)
  6070. | SyntaxTree.IfStatement DO VisitIfStatement(x)
  6071. | SyntaxTree.WithStatement DO VisitWithStatement(x)
  6072. | SyntaxTree.CaseStatement DO VisitCaseStatement(x)
  6073. | SyntaxTree.WhileStatement DO VisitWhileStatement(x)
  6074. | SyntaxTree.RepeatStatement DO VisitRepeatStatement(x)
  6075. | SyntaxTree.ForStatement DO VisitForStatement(x)
  6076. | SyntaxTree.LoopStatement DO VisitLoopStatement(x)
  6077. | SyntaxTree.ExitableBlock DO VisitExitableBlock(x)
  6078. | SyntaxTree.ExitStatement DO VisitExitStatement(x)
  6079. | SyntaxTree.ReturnStatement DO VisitReturnStatement(x)
  6080. | SyntaxTree.AwaitStatement DO VisitAwaitStatement(x)
  6081. | SyntaxTree.StatementBlock DO VisitStatementBlock(x)
  6082. | SyntaxTree.Code DO VisitCode(x)
  6083. END;
  6084. END VStatement;
  6085. PROCEDURE ResolveStatement*(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6086. VAR prev,resolved: SyntaxTree.Statement;
  6087. BEGIN
  6088. prev := resolvedStatement;
  6089. resolvedStatement := x;
  6090. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6091. activeCellsStatement := FALSE;
  6092. VStatement(x);
  6093. (* removed this, implementation restriction should be resolved by backend
  6094. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6095. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6096. END;
  6097. *)
  6098. resolved := resolvedStatement;
  6099. resolvedStatement := prev;
  6100. RETURN resolved
  6101. END ResolveStatement;
  6102. (** check and resolve statement sequence
  6103. - check all statements, replace if necessary
  6104. **)
  6105. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6106. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6107. BEGIN
  6108. IF statementSequence # NIL THEN (* else empty *)
  6109. FOR i := 0 TO statementSequence.Length()-1 DO
  6110. statement := statementSequence.GetStatement(i);
  6111. resolved := ResolveStatement(statement);
  6112. IF (resolved # statement) THEN
  6113. statementSequence.SetStatement(i,resolved);
  6114. END;
  6115. END;
  6116. END;
  6117. END StatementSequence;
  6118. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6119. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6120. - check if procedure is callable
  6121. - check return type = NIL (otherwise must be assignment statement)
  6122. **)
  6123. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6124. VAR call: SyntaxTree.Designator;
  6125. BEGIN
  6126. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6127. call := procedureCall.call;
  6128. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6129. IF procedureCall.ignore THEN Error(procedureCall.position, "ignoring non-procedure call") END;
  6130. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6131. END;
  6132. call := ResolveDesignator(call);
  6133. IF call = SyntaxTree.invalidDesignator THEN
  6134. (* error already handled *)
  6135. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6136. (* inline call in a statement *)
  6137. ELSIF ~IsCallable(call) THEN
  6138. Error(procedureCall.position,"called object is not a procedure");
  6139. ELSIF call.type # NIL THEN
  6140. IF ~procedureCall.ignore THEN
  6141. Error(procedureCall.position,"calling procedure with non-void return type");
  6142. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6143. END;
  6144. ELSIF procedureCall.ignore THEN
  6145. Error(procedureCall.position,"ignoring procedure call without return value");
  6146. END;
  6147. procedureCall.SetCall(call);
  6148. END VisitProcedureCallStatement;
  6149. (** check and resolve assignment LHS := RHS
  6150. - resolve LHS and RHS
  6151. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6152. - check if assignment is compatible
  6153. - check if LHS is variable (i.e. assignable)
  6154. - convert RHS if necessary
  6155. **)
  6156. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  6157. VAR
  6158. left: SyntaxTree.Designator;
  6159. right, expression: SyntaxTree.Expression;
  6160. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6161. BEGIN
  6162. right := ResolveExpression(assignment.right);
  6163. assignment.left.SetRelatedRhs(right);
  6164. left := ResolveDesignator(assignment.left);
  6165. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6166. (* error already handled *)
  6167. ELSIF IsIndexOperator(left) & left.assignable THEN
  6168. (* LHS is index write operator call *)
  6169. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6170. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, FALSE, procedureCallDesignator, assignment.outer);
  6171. ELSIF CheckVariable(left) THEN
  6172. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6173. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6174. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6175. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, FALSE, procedureCallDesignator, assignment.outer);
  6176. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6177. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6178. ELSIF AssignmentCompatible(left, right) THEN
  6179. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6180. assignment.SetLeft(left);
  6181. assignment.SetRight(right);
  6182. resolvedStatement := assignment
  6183. END
  6184. END
  6185. END VisitAssignment;
  6186. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  6187. VAR
  6188. left: SyntaxTree.Designator;
  6189. right: SyntaxTree.Expression;
  6190. inPort, outPort: SyntaxTree.PortType;
  6191. expression: SyntaxTree.Expression;
  6192. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6193. BEGIN
  6194. right := ResolveExpression(communication.right);
  6195. left := ResolveDesignator(communication.left);
  6196. communication.SetLeft(left);
  6197. communication.SetRight(right);
  6198. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6199. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6200. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6201. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, FALSE, procedureCallDesignator, communication.outer);
  6202. ELSE
  6203. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6204. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6205. (* error already handled *)
  6206. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6207. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6208. IF outPort.direction # SyntaxTree.OutPort THEN
  6209. Error(left.position,"not an out-port")
  6210. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6211. Error(left.position,"incompatible to port type");
  6212. ELSE
  6213. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6214. communication.SetRight(right)
  6215. END;
  6216. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6217. IF CheckVariable(left) THEN
  6218. IF inPort.direction # SyntaxTree.InPort THEN
  6219. Error(left.position,"not an in-port")
  6220. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6221. Error(right.position,"incompatible to port type");
  6222. END;
  6223. END;
  6224. ELSE
  6225. Error(communication.position,"unsupported stream operation");
  6226. END;
  6227. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6228. IF outPort.direction # SyntaxTree.OutPort THEN
  6229. Error(left.position,"not an out-port")
  6230. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6231. Error(left.position,"incompatible to port type");
  6232. ELSE
  6233. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6234. communication.SetRight(right)
  6235. END;
  6236. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6237. IF CheckVariable(right) THEN
  6238. IF inPort.direction # SyntaxTree.InPort THEN
  6239. Error(left.position,"not an in-port")
  6240. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6241. Error(right.position,"incompatible to port type");
  6242. END;
  6243. END;
  6244. ELSE
  6245. Error(communication.position, "unsupported operation");
  6246. END;
  6247. END;
  6248. END VisitCommunicationStatement;
  6249. (** check and resolve if/eslif part
  6250. - check condition
  6251. - check statement sequence
  6252. **)
  6253. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6254. VAR prevUnreachable, b: BOOLEAN;
  6255. BEGIN
  6256. prevUnreachable := currentIsUnreachable;
  6257. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6258. IF IsBooleanValue(ifPart.condition,b) THEN
  6259. IF b=FALSE THEN
  6260. currentIsUnreachable := TRUE
  6261. ELSIF b=TRUE THEN
  6262. true := TRUE
  6263. END;
  6264. END;
  6265. StatementSequence(ifPart.statements);
  6266. currentIsUnreachable := prevUnreachable;
  6267. END IfPart;
  6268. (** check and resolve if statement
  6269. - check if parts and else part statement sequence
  6270. **)
  6271. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  6272. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6273. BEGIN
  6274. prevUnreachable := currentIsUnreachable;
  6275. ifPartTrue := FALSE;
  6276. IfPart(ifStatement.ifPart,ifPartTrue);
  6277. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6278. elsif := ifStatement.GetElsifPart(i);
  6279. IfPart(elsif,ifPartTrue);
  6280. END;
  6281. IF ifStatement.elsePart # NIL THEN
  6282. IF ifPartTrue THEN
  6283. currentIsUnreachable := TRUE
  6284. END;
  6285. StatementSequence(ifStatement.elsePart)
  6286. END;
  6287. currentIsUnreachable := prevUnreachable;
  6288. END VisitIfStatement;
  6289. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; variable: SyntaxTree.Designator);
  6290. VAR
  6291. type,variableType: SyntaxTree.Type;
  6292. withEntry: WithEntry;
  6293. symbol: SyntaxTree.Symbol
  6294. BEGIN
  6295. type := ResolveType(withPart.type);
  6296. withPart.SetType(type);
  6297. variableType := variable.type.resolved;
  6298. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6299. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6300. END;
  6301. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6302. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6303. Error(variable.position,"is not extensible designator");
  6304. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6305. Error(variable.position,"is no local variable ");
  6306. IF VerboseErrorMessage THEN
  6307. Printout.Info("variable",variable)
  6308. END;
  6309. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6310. Error(variable.position,"withguarded symbol is no type extension of ");
  6311. IF VerboseErrorMessage THEN
  6312. Printout.Info("variable",variable);
  6313. Printout.Info("type",type);
  6314. END;
  6315. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6316. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6317. Error(variable.position,"withguarded symbol is no variable ");
  6318. IF VerboseErrorMessage THEN
  6319. Printout.Info("variable",variable);
  6320. Printout.Info("type",type);
  6321. END;
  6322. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6323. Error(variable.position,"invalid change of withguarded symbol");
  6324. ELSE
  6325. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6326. NEW(withEntry);
  6327. withEntry.previous := withEntries;
  6328. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6329. withEntry.type := type;
  6330. withEntries := withEntry;
  6331. StatementSequence(withPart.statements);
  6332. withEntries := withEntries.previous;
  6333. END;
  6334. END WithPart;
  6335. (** check and resolve with statement WITH variable: type DO ... END;
  6336. - check type and variable
  6337. - check that variable type is type extension of type
  6338. - check that variable is a variable
  6339. - enter new with scope and enter guardedVariable with same name and reference to variable
  6340. - create if statement:
  6341. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6342. **)
  6343. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  6344. VAR i,j: LONGINT; prevScope: SyntaxTree.Scope; variable: SyntaxTree.Designator;
  6345. BEGIN
  6346. prevScope := currentScope;
  6347. variable := ResolveDesignator(withStatement.variable);
  6348. withStatement.SetVariable(variable);
  6349. FOR i := 0 TO withStatement.WithParts()-1 DO
  6350. WithPart(withStatement.GetWithPart(i),variable);
  6351. END;
  6352. FOR i := 0 TO withStatement.WithParts()-1 DO
  6353. FOR j := i+1 TO withStatement.WithParts()-1 DO
  6354. IF IsTypeExtension(withStatement.GetWithPart(i).type, withStatement.GetWithPart(j).type) THEN
  6355. Error(withStatement.GetWithPart(j).position, "unreachable extended type");
  6356. END;
  6357. END;
  6358. END;
  6359. IF withStatement.elsePart # NIL THEN
  6360. StatementSequence(withStatement.elsePart)
  6361. END;
  6362. currentScope := prevScope;
  6363. END VisitWithStatement;
  6364. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6365. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6366. - check 'first' < 'last' and no overlaps between different case labels
  6367. - check statement sequence
  6368. **)
  6369. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  6370. VAR
  6371. i: LONGINT;
  6372. position: Position;
  6373. expression, left, right: SyntaxTree.Expression;
  6374. expressionType: SyntaxTree.Type;
  6375. l, r: Basic.Integer;
  6376. cl, cr: CHAR;
  6377. thiscases: SyntaxTree.CaseConstant;
  6378. BEGIN
  6379. thiscases := NIL;
  6380. FOR i := 0 TO casePart.elements.Length() - 1 DO
  6381. expression := casePart.elements.GetExpression(i);
  6382. position := expression.position;
  6383. (* set context of range *)
  6384. IF expression IS SyntaxTree.RangeExpression THEN
  6385. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  6386. END;
  6387. expression := ResolveExpression(expression);
  6388. IF expression = SyntaxTree.invalidExpression THEN
  6389. (* error already reported *)
  6390. expressionType := SyntaxTree.invalidType;
  6391. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  6392. (* read out 'first' and 'last' *)
  6393. left := expression(SyntaxTree.RangeExpression).first;
  6394. right := expression(SyntaxTree.RangeExpression).last;
  6395. (* guaranteed by VisitRangeExpression: *)
  6396. ASSERT((left # NIL) & (right # NIL));
  6397. ASSERT(left.type.resolved = right.type.resolved);
  6398. left := CompatibleConversion(left.position, left, type);
  6399. right := CompatibleConversion(right.position, right, type);
  6400. expression(SyntaxTree.RangeExpression).SetFirst(left);
  6401. expression(SyntaxTree.RangeExpression).SetLast(right);
  6402. expressionType := RegularType(position,left.type);
  6403. ELSE
  6404. expression := ConstantExpression(expression);
  6405. expression := CompatibleConversion(expression.position, expression, type);
  6406. (*
  6407. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  6408. left := Global.NewCharacterValue(system,expression.position,cl);
  6409. expression := casePart.elements.GetExpression(i);
  6410. expression.SetResolved(left(SyntaxTree.CharacterValue));
  6411. expression := left
  6412. END;
  6413. *)
  6414. casePart.elements.SetExpression(i,expression);
  6415. left := expression; right := expression;
  6416. expressionType := RegularType(position,expression.type)
  6417. END;
  6418. IF (expressionType = SyntaxTree.invalidType) THEN
  6419. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  6420. Error(position, "inadmissible case label");
  6421. expression := SyntaxTree.invalidExpression;
  6422. ELSE
  6423. l := 0; r := 0;
  6424. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  6425. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  6426. l := ORD(cl); r := ORD(cr);
  6427. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  6428. ELSE
  6429. expression := SyntaxTree.invalidExpression
  6430. END;
  6431. IF expression # SyntaxTree.invalidExpression THEN
  6432. IF l>r THEN
  6433. Error(position, "empty case label")
  6434. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  6435. Error(position, "duplicate case label");
  6436. ELSE
  6437. IF l < min THEN min := l END;
  6438. IF r > max THEN max := r END;
  6439. END;
  6440. END;
  6441. END;
  6442. casePart.elements.SetExpression(i,expression);
  6443. END;
  6444. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  6445. casePart.SetConstants(thiscases);
  6446. StatementSequence(casePart.statements);
  6447. END CasePart;
  6448. (** check and resolve case statement CASE variable OF ... END;
  6449. - check variable
  6450. - check case parts
  6451. **)
  6452. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  6453. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  6454. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer;
  6455. BEGIN
  6456. expression := ResolveExpression(caseStatement.variable);
  6457. type := RegularType(expression.position,expression.type);
  6458. IF type = SyntaxTree.invalidType THEN
  6459. expression := SyntaxTree.invalidExpression;
  6460. ELSIF IsIntegerType(type) THEN
  6461. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  6462. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  6463. (*
  6464. expression := Global.NewCharacterValue(system,expression.position,ch);
  6465. *)
  6466. type := expression.type;
  6467. ELSIF IsCharacterType(type) THEN
  6468. ELSIF IsEnumerationType(type) THEN
  6469. ELSE
  6470. Error(caseStatement.variable.position,"variable must be integer or character type");
  6471. expression := SyntaxTree.invalidExpression;
  6472. END;
  6473. caseStatement.SetVariable(expression);
  6474. caseList := NIL;
  6475. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  6476. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  6477. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  6478. END;
  6479. caseStatement.SetMinMax(min,max);
  6480. StatementSequence(caseStatement.elsePart);
  6481. IF expression.resolved # NIL THEN
  6482. IF IsCharacterValue(expression,ch) THEN
  6483. l := ORD(ch)
  6484. ELSIF IsIntegerValue(expression,l) THEN
  6485. END;
  6486. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  6487. END;
  6488. END VisitCaseStatement;
  6489. (** check and resolve while statement
  6490. - check condition
  6491. - check statement sequence
  6492. **)
  6493. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  6494. VAR prevIsUnreachable,b: BOOLEAN;
  6495. BEGIN
  6496. prevIsUnreachable := currentIsUnreachable;
  6497. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  6498. IF IsBooleanValue(whileStatement.condition,b) THEN
  6499. IF b=FALSE THEN
  6500. currentIsUnreachable := TRUE
  6501. END;
  6502. END;
  6503. StatementSequence(whileStatement.statements);
  6504. currentIsUnreachable := prevIsUnreachable
  6505. END VisitWhileStatement;
  6506. (** check and resolve repeat statement
  6507. - check condition
  6508. - check statement sequence
  6509. **)
  6510. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  6511. BEGIN
  6512. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  6513. StatementSequence(repeatStatement.statements);
  6514. END VisitRepeatStatement;
  6515. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  6516. VAR withEntry: WithEntry;
  6517. BEGIN
  6518. withEntry := withEntries;
  6519. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  6520. withEntry := withEntry.previous
  6521. END;
  6522. IF withEntry = NIL THEN RETURN FALSE
  6523. ELSE
  6524. type := withEntry.type;
  6525. RETURN TRUE
  6526. END;
  6527. END GetGuard;
  6528. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  6529. - check that variable is an integer variable
  6530. - check that from is integer typed with compatible type
  6531. - check that to has compatible type
  6532. - check that by is constant integer with compatible type
  6533. **)
  6534. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  6535. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  6536. BEGIN
  6537. designator := ResolveDesignator(forStatement.variable);
  6538. type := SyntaxTree.invalidType;
  6539. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  6540. designator := SyntaxTree.invalidDesignator;
  6541. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  6542. Error(designator.position,"control variable of non-integer type");
  6543. designator := SyntaxTree.invalidDesignator;
  6544. ELSIF CheckVariable(designator) THEN
  6545. type := designator.type;
  6546. END;
  6547. forStatement.SetVariable(designator);
  6548. expression := ResolveExpression(forStatement.from);
  6549. IF expression = SyntaxTree.invalidExpression THEN
  6550. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  6551. Error(expression.position,"start value of incompatible type");
  6552. expression := SyntaxTree.invalidExpression;
  6553. ELSIF type # SyntaxTree.invalidType THEN
  6554. expression := NewConversion(expression.position,expression,type,NIL)
  6555. END;
  6556. forStatement.SetFrom(expression);
  6557. expression := ResolveExpression(forStatement.to);
  6558. IF expression = SyntaxTree.invalidExpression THEN
  6559. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  6560. Error(expression.position,"end value of incompatible type");
  6561. expression := SyntaxTree.invalidExpression;
  6562. ELSIF type # SyntaxTree.invalidType THEN
  6563. expression := NewConversion(expression.position,expression,type,NIL)
  6564. END;
  6565. forStatement.SetTo(expression);
  6566. IF forStatement.by # NIL THEN
  6567. expression := ConstantInteger(forStatement.by);
  6568. ELSE
  6569. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  6570. END;
  6571. IF expression = SyntaxTree.invalidExpression THEN
  6572. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  6573. Error(expression.position,"step value of incompatible type");
  6574. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  6575. Error(expression.position,"invalid step value");
  6576. ELSIF type # SyntaxTree.invalidType THEN
  6577. expression := NewConversion(expression.position,expression,type,NIL)
  6578. END;
  6579. forStatement.SetBy(expression);
  6580. StatementSequence(forStatement.statements);
  6581. END VisitForStatement;
  6582. (** check and resolve loop statement LOOP StatementSequence END
  6583. - check statement sequence
  6584. **)
  6585. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  6586. BEGIN
  6587. StatementSequence(loopStatement.statements)
  6588. END VisitLoopStatement;
  6589. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  6590. BEGIN
  6591. StatementSequence(exitableBlock.statements);
  6592. END VisitExitableBlock;
  6593. (** check and resolve exit statement EXIT
  6594. - check that exit is within LOOP statement block
  6595. **)
  6596. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  6597. VAR outer: SyntaxTree.Statement;
  6598. BEGIN
  6599. outer := exitStatement.outer;
  6600. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  6601. outer := outer.outer;
  6602. END;
  6603. IF outer = NIL THEN
  6604. Error(exitStatement.position,"exit statement not within loop statement");
  6605. END;
  6606. END VisitExitStatement;
  6607. (** check and resolve return statement RETURN [expression]
  6608. - check expression (if any)
  6609. - check if in procedure scope
  6610. - if in procedure scope then check expression compatibility
  6611. - if not in procecdure scope then check on return without expression
  6612. **)
  6613. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  6614. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  6615. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  6616. BEGIN
  6617. position := returnStatement.position;
  6618. expression := returnStatement.returnValue;
  6619. IF expression # NIL THEN
  6620. expression := ResolveExpression(expression);
  6621. returnStatement.SetReturnValue(expression);
  6622. END;
  6623. outer := returnStatement.outer;
  6624. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  6625. outer := outer.outer
  6626. END;
  6627. IF (outer # NIL) THEN
  6628. scope := outer(SyntaxTree.Body).inScope;
  6629. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  6630. IF (expression # NIL) THEN
  6631. Error(position, "return statement with parameter not in procedure scope");
  6632. END;
  6633. ELSE
  6634. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  6635. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  6636. Error(position, "return statement in procedure that does not return");
  6637. END;
  6638. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  6639. IF returnType # NIL THEN
  6640. returnType := returnType.resolved;
  6641. IF expression = NIL THEN
  6642. Error(position, "empty return type in procedure providing a return type")
  6643. ELSIF expression.type = NIL THEN
  6644. Error(position,"returned type incompatible: expression has no type");
  6645. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  6646. Error(position, "return type not compatible");
  6647. IF VerboseErrorMessage THEN
  6648. Printout.Info("returnType",returnType);
  6649. Printout.Info("expression",expression);
  6650. END;
  6651. ELSE
  6652. expression := NewConversion(expression.position,expression,returnType,NIL);
  6653. returnStatement.SetReturnValue(expression);
  6654. END;
  6655. ELSIF expression # NIL THEN
  6656. Error(position, "non-empty return type in procedure providing no return type");
  6657. END;
  6658. END;
  6659. END;
  6660. END VisitReturnStatement;
  6661. (** check and resolve await statement AWAIT(condition: Expression)
  6662. - check await condition
  6663. **)
  6664. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  6665. VAR condition: SyntaxTree.Expression;
  6666. BEGIN
  6667. condition := ResolveCondition(awaitStatement.condition);
  6668. IF currentIsRealtime THEN
  6669. Error(awaitStatement.position,"forbidden await statement in realtime block");
  6670. END;
  6671. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  6672. Error(awaitStatement.position,"senseless await statement with constant condition");
  6673. END;
  6674. awaitStatement.SetCondition(condition);
  6675. END VisitAwaitStatement;
  6676. PROCEDURE CheckSystemImport(position: Position);
  6677. VAR import: SyntaxTree.Import;
  6678. BEGIN
  6679. import := currentScope.ownerModule.moduleScope.firstImport;
  6680. WHILE(import # NIL) DO
  6681. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  6682. RETURN;
  6683. END;
  6684. import := import.nextImport;
  6685. END;
  6686. Error(position, "forbidden code without system import");
  6687. END CheckSystemImport;
  6688. (** check and resolve code statement: do nothing, must be done by assembler
  6689. **)
  6690. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  6691. VAR i: LONGINT; statement: SyntaxTree.Statement;
  6692. BEGIN
  6693. CheckSystemImport(code.position);
  6694. FOR i := 0 TO code.inRules.Length()-1 DO
  6695. statement := code.inRules.GetStatement(i);
  6696. IF statement IS SyntaxTree.Assignment THEN
  6697. WITH statement: SyntaxTree.Assignment DO
  6698. statement.SetRight(ResolveExpression(statement.right));
  6699. END;
  6700. ELSE
  6701. Error(statement.position, "can only be assignment")
  6702. END;
  6703. END;
  6704. FOR i := 0 TO code.outRules.Length()-1 DO
  6705. statement := code.outRules.GetStatement(i);
  6706. IF statement IS SyntaxTree.Assignment THEN
  6707. WITH statement: SyntaxTree.Assignment DO
  6708. statement.SetLeft(ResolveDesignator(statement.left));
  6709. END;
  6710. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  6711. (* must be a reference to some register *)
  6712. ELSIF statement IS SyntaxTree.StatementBlock THEN
  6713. ELSE
  6714. Printout.Info("out statement ", statement);
  6715. Error(statement.position, "(out) can only be assignment")
  6716. END;
  6717. END;
  6718. END VisitCode;
  6719. (** check and set flags of a statement block
  6720. - check for multiply occurence of a flag
  6721. - check and set priority only in bodies
  6722. - check for valid names
  6723. **)
  6724. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  6725. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  6726. flag: LONGINT; recordBody: SyntaxTree.Body;
  6727. PROCEDURE SetProtectedRecord;
  6728. VAR scope: SyntaxTree.Scope;
  6729. BEGIN
  6730. scope := currentScope;
  6731. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  6732. scope := scope.outerScope
  6733. END;
  6734. IF scope # NIL THEN
  6735. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  6736. END;
  6737. END SetProtectedRecord;
  6738. BEGIN
  6739. flags := {};
  6740. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  6741. recordBody := block(SyntaxTree.Body)
  6742. ELSE
  6743. recordBody := NIL
  6744. END;
  6745. blockModifier := block.blockModifiers;
  6746. WHILE(blockModifier # NIL) DO
  6747. name := blockModifier.identifier;
  6748. expression := blockModifier.expression;
  6749. position := blockModifier.position;
  6750. flag := -1;
  6751. IF name=Global.NamePriority THEN
  6752. IF expression = NIL THEN
  6753. Error(position, "missing priority expression");
  6754. ELSIF recordBody = NIL THEN
  6755. Error(position, "priority not on record body");
  6756. ELSIF recordBody.priority # NIL THEN
  6757. Error(position, "duplicate priority expression");
  6758. ELSE
  6759. recordBody.SetPriority(expression);
  6760. END;
  6761. ELSIF expression # NIL THEN
  6762. Error(expression.position,"expression not in connection with priority")
  6763. ELSIF name=Global.NameExclusive THEN
  6764. IF block.isExclusive THEN
  6765. Error(position, "duplicate exclusive flag")
  6766. END;
  6767. block.SetExclusive(TRUE); SetProtectedRecord;
  6768. ELSIF name=Global.NameActive THEN
  6769. IF recordBody = NIL THEN
  6770. Error(position, "active not in record body");
  6771. ELSIF recordBody.isActive THEN
  6772. Error(position, "duplicate active flag")
  6773. ELSE
  6774. recordBody.SetActive(TRUE); SetProtectedRecord;
  6775. END;
  6776. ELSIF name=Global.NameSafe THEN
  6777. IF recordBody = NIL THEN
  6778. Error(position, "safe not in record body");
  6779. ELSIF recordBody.isSafe THEN
  6780. Error(position, "duplicate safe flag")
  6781. ELSE
  6782. recordBody.SetSafe(TRUE);
  6783. SetProtectedRecord;
  6784. END;
  6785. ELSIF name=Global.NameRealtime THEN
  6786. IF recordBody = NIL THEN
  6787. Error(position, "realtime not in record body");
  6788. ELSIF recordBody.isRealtime THEN
  6789. Error(position, "duplicate realtime flag")
  6790. ELSE
  6791. recordBody.SetRealtime(TRUE);
  6792. block.SetRealtime(TRUE);
  6793. END;
  6794. ELSIF name=Global.NameUnchecked THEN
  6795. IF block.isUnchecked THEN
  6796. Error(position, "duplicate unchecked flag")
  6797. ELSE
  6798. block.SetUnchecked(TRUE);
  6799. END;
  6800. ELSIF (name=Global.NameUncooperative) THEN
  6801. IF block.isUncooperative THEN
  6802. Error(position, "duplicate uncooperative flag")
  6803. ELSE
  6804. block.SetUncooperative(TRUE);
  6805. END;
  6806. ELSE
  6807. Error(position, "unknown block modifier");
  6808. END;
  6809. blockModifier := blockModifier.nextModifier;
  6810. END;
  6811. END BlockFlags;
  6812. (** check and resolve statement block
  6813. - check flags (exclusive)
  6814. - check statement sequence
  6815. **)
  6816. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  6817. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN; recentScope: SyntaxTree.Scope;
  6818. BEGIN
  6819. recentScope := currentScope;
  6820. (* IF statementBlock.scope # NIL THEN currentScope := statementBlock.scope END;*)
  6821. BlockFlags(statementBlock);
  6822. IF statementBlock.isExclusive THEN
  6823. (* check that not in exclusive block *)
  6824. IF currentIsExclusive THEN
  6825. Error (statementBlock.position,"forbidden recursive exclusive")
  6826. ELSIF currentIsRealtime THEN
  6827. Error( statementBlock.position,"forbidden exculsive in realtime block");
  6828. END;
  6829. END;
  6830. recentExclusive := currentIsExclusive;
  6831. recentUnreachable := currentIsUnreachable;
  6832. recentRealtime := currentIsRealtime;
  6833. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  6834. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  6835. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  6836. StatementSequence(statementBlock.statements);
  6837. currentIsRealtime := recentRealtime;
  6838. currentIsExclusive := recentExclusive;
  6839. currentIsUnreachable := recentUnreachable;
  6840. currentScope := recentScope;
  6841. END VisitStatementBlock;
  6842. (** check and resolve body
  6843. - check flags (active, priority, safe)
  6844. - check body and finally part
  6845. **)
  6846. PROCEDURE Body(body: SyntaxTree.Body);
  6847. BEGIN
  6848. VisitStatementBlock(body);
  6849. IF body.isActive THEN
  6850. IF ~currentIsBodyProcedure THEN
  6851. Error(body.position,"active flag not in object body");
  6852. ELSIF body.priority # NIL THEN
  6853. body.SetPriority(ConstantInteger(body.priority));
  6854. END;
  6855. ELSIF body.isSafe THEN
  6856. Error(body.position,"safe flag not in active body");
  6857. ELSIF body.priority # NIL THEN
  6858. Error(body.position,"priority flag not in active body");
  6859. END;
  6860. IF body.code # NIL THEN
  6861. CheckSystemImport(body.position);
  6862. END;
  6863. StatementSequence(body.finally)
  6864. END Body;
  6865. (*** scopes ***)
  6866. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  6867. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  6868. VAR duplicateSymbol: BOOLEAN;
  6869. BEGIN
  6870. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  6871. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  6872. Error(symbol.position,"globally defined keyword")
  6873. END;
  6874. scope.EnterSymbol(symbol,duplicateSymbol);
  6875. IF ~allowDuplicate & duplicateSymbol THEN
  6876. Error(symbol.position,"Multiply defined identifier.");
  6877. IF VerboseErrorMessage THEN
  6878. Printout.Info("multiply defined identifier",symbol);
  6879. Printout.Info("in scope",scope);
  6880. END;
  6881. END;
  6882. END Register;
  6883. (**
  6884. implementation: check and resolve an implementation part
  6885. **)
  6886. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  6887. move implementation checker to a separate object ? *)
  6888. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  6889. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  6890. procedureType: SyntaxTree.ProcedureType;
  6891. BEGIN
  6892. prevIsRealtime := currentIsRealtime;
  6893. prevIsBodyProcedure := currentIsBodyProcedure;
  6894. prevIsCellNet := currentIsCellNet;
  6895. prevScope := currentScope;
  6896. currentScope := scope;
  6897. WITH scope: SyntaxTree.ProcedureScope DO
  6898. procedure := scope.ownerProcedure;
  6899. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6900. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  6901. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  6902. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  6903. (*
  6904. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  6905. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  6906. END;
  6907. *)
  6908. IF scope.body # NIL THEN
  6909. (* module body, record bodies are wrapped into an artifical procedure *)
  6910. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  6911. Body(scope(SyntaxTree.ProcedureScope).body)
  6912. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  6913. Body(scope(SyntaxTree.ProcedureScope).body)
  6914. END;
  6915. END;
  6916. IF procedureType.noPAF THEN
  6917. IF scope.firstVariable # NIL THEN
  6918. Error(procedure.position, "forbidden variable in procedure without activation frame");
  6919. ELSIF procedureType.firstParameter # NIL THEN
  6920. Error(procedure.position, "forbidden parameter in procedure without activation frame");
  6921. END;
  6922. END;
  6923. ELSE
  6924. END;
  6925. currentScope := prevScope;
  6926. currentIsRealtime := prevIsRealtime;
  6927. currentIsBodyProcedure := prevIsBodyProcedure;
  6928. currentIsCellNet := prevIsCellNet;
  6929. END Implementation;
  6930. (** implementation phase:
  6931. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  6932. **)
  6933. PROCEDURE Implementations(x: SyntaxTree.Module);
  6934. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  6935. BEGIN
  6936. prevPhase := phase;
  6937. phase := InlinePhase;
  6938. scope := x.firstScope;
  6939. WHILE(scope # NIL) DO
  6940. Implementation(scope);
  6941. scope := scope.nextScope;
  6942. END;
  6943. phase := ImplementationPhase;
  6944. scope := x.firstScope;
  6945. WHILE(scope # NIL) DO
  6946. Implementation(scope);
  6947. scope := scope.nextScope;
  6948. END;
  6949. phase := prevPhase;
  6950. END Implementations;
  6951. (** declaration phase:
  6952. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  6953. - import lists (for module scopes)
  6954. - parameter list (for procedure scopes)
  6955. - constant declarations
  6956. - type declarations
  6957. - variable declarations
  6958. - procedure declarations
  6959. preformed in two stages:
  6960. - first all symbols are entered into the symbol table (with uniqueness check),
  6961. - then all symbols are resolved
  6962. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  6963. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  6964. phases :
  6965. 0 = before procedures
  6966. 1 = procedures and later
  6967. **)
  6968. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  6969. VAR
  6970. constant: SyntaxTree.Constant;
  6971. typeDeclaration: SyntaxTree.TypeDeclaration;
  6972. declaredType: SyntaxTree.Type;
  6973. variable: SyntaxTree.Variable;
  6974. procedure: SyntaxTree.Procedure;
  6975. procedureType : SyntaxTree.ProcedureType;
  6976. prevScope: SyntaxTree.Scope;
  6977. parameter: SyntaxTree.Parameter;
  6978. import: SyntaxTree.Import;
  6979. symbol: SyntaxTree.Symbol;
  6980. prevPhase: LONGINT;
  6981. prevError : BOOLEAN;
  6982. i: LONGINT;
  6983. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  6984. VAR baseType: SyntaxTree.Type; property: SyntaxTree.Property;
  6985. BEGIN
  6986. IF type.baseType # NIL THEN
  6987. baseType := type.baseType.resolved;
  6988. IF baseType IS SyntaxTree.PointerType THEN
  6989. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  6990. END;
  6991. (*
  6992. IF baseType IS SyntaxTree.CellType THEN
  6993. DeclareCell(baseType(SyntaxTree.CellType));
  6994. END;
  6995. *)
  6996. END;
  6997. parameter := type.firstParameter;
  6998. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  6999. (*
  7000. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7001. variable.SetType(parameter.type);
  7002. variable.SetAccess(SyntaxTree.Hidden);
  7003. variable.SetModifiers(parameter.modifiers);
  7004. currentScope.PushVariable(variable);
  7005. *)
  7006. Register(parameter,scope, FALSE);
  7007. parameter := parameter.nextParameter;
  7008. END;
  7009. property := type.firstProperty;
  7010. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7011. (*
  7012. variable := currentScope.FindVariable(property.name);
  7013. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7014. prop := variable(SyntaxTree.Property);
  7015. ELSE (* add, duplicate symbols detection later *)
  7016. prop := SyntaxTree.NewProperty(property.position, property.name);
  7017. currentScope.PushVariable(prop);
  7018. END;
  7019. prop.SetType(property.type);
  7020. prop.SetValue(property.value);
  7021. prop.SetAccess(SyntaxTree.Hidden);
  7022. *)
  7023. Register(property, scope, FALSE);
  7024. property := property.nextProperty;
  7025. END;
  7026. END DeclareCell;
  7027. BEGIN
  7028. prevError := error;
  7029. prevPhase := phase;
  7030. phase := DeclarationPhase;
  7031. prevScope := currentScope;
  7032. currentScope := scope;
  7033. error := FALSE;
  7034. IF 0 IN phases THEN
  7035. (* first enter all symbols in scope *)
  7036. IF scope IS SyntaxTree.ModuleScope THEN
  7037. (* treat imports first for a module scope, , set default context if necessary *)
  7038. import := scope(SyntaxTree.ModuleScope).firstImport;
  7039. WHILE(import # NIL) DO
  7040. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7041. Register(import, currentScope, FALSE);
  7042. import := import.nextImport;
  7043. END;
  7044. import := scope(SyntaxTree.ModuleScope).firstImport;
  7045. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7046. ResolveSymbol(import);
  7047. import := import.nextImport;
  7048. END;
  7049. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7050. (* enter parameters for a procedure scope *)
  7051. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7052. parameter := procedureType.firstParameter;
  7053. WHILE(parameter # NIL) DO
  7054. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7055. END;
  7056. parameter := procedureType.returnParameter;
  7057. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7058. parameter := procedureType.selfParameter;
  7059. IF parameter # NIL THEN
  7060. Register(parameter, currentScope, FALSE);
  7061. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7062. END;
  7063. ELSIF scope IS SyntaxTree.CellScope THEN
  7064. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7065. IF~skipImplementation THEN
  7066. import := scope(SyntaxTree.CellScope).firstImport;
  7067. WHILE(import # NIL) DO
  7068. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7069. Register(import, currentScope, FALSE);
  7070. import := import.nextImport;
  7071. END;
  7072. import := scope(SyntaxTree.CellScope).firstImport;
  7073. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7074. ResolveSymbol(import);
  7075. import := import.nextImport;
  7076. END;
  7077. END;
  7078. END;
  7079. IF error THEN RETURN END;
  7080. IF skipImplementation THEN
  7081. scope.Clear;
  7082. END;
  7083. (* constants *)
  7084. constant := scope.firstConstant;
  7085. WHILE (constant # NIL) DO
  7086. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7087. END;
  7088. (* type declarations *)
  7089. typeDeclaration := scope.firstTypeDeclaration;
  7090. WHILE (typeDeclaration # NIL) DO
  7091. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7092. END;
  7093. (* variables *)
  7094. variable := scope.firstVariable;
  7095. WHILE (variable # NIL) DO
  7096. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7097. END;
  7098. (* procedures *)
  7099. IF scope.procedures # NIL THEN
  7100. FOR i := 0 TO scope.procedures.Length()-1 DO
  7101. procedure := scope.procedures.GetProcedure(i);
  7102. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7103. IF procedureType.selfParameter = NIL THEN
  7104. scope.AddProcedure(procedure);
  7105. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7106. ELSE
  7107. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7108. IF typeDeclaration = NIL THEN
  7109. Error(procedureType.selfParameter.position, "No such type declaration");
  7110. ELSE
  7111. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7112. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7113. declaredType := typeDeclaration.declaredType;
  7114. IF declaredType IS SyntaxTree.PointerType THEN
  7115. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7116. END;
  7117. IF declaredType IS SyntaxTree.RecordType THEN
  7118. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7119. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7120. ELSE
  7121. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7122. END;
  7123. END;
  7124. END;
  7125. END;
  7126. END;
  7127. END;
  7128. (* now process all symbols without any presumption on the order *)
  7129. symbol := scope.firstSymbol;
  7130. WHILE(symbol # NIL) DO
  7131. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7132. IF (symbol IS SyntaxTree.Procedure) THEN
  7133. IF 1 IN phases THEN
  7134. ResolveSymbol(symbol);
  7135. END;
  7136. ELSE
  7137. IF 0 IN phases THEN
  7138. ResolveSymbol(symbol);
  7139. END;
  7140. END;
  7141. END;
  7142. symbol := symbol.nextSymbol;
  7143. END;
  7144. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7145. symbol := scope.firstSymbol;
  7146. WHILE symbol # NIL DO
  7147. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7148. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7149. pointerFixes.Add(symbol, currentScope);
  7150. END;
  7151. IF ~symbol.type.resolved.isRealtime THEN
  7152. Error(symbol.position,"symbol has no realtime type");
  7153. END;
  7154. END;
  7155. symbol := symbol.nextSymbol
  7156. END;
  7157. END;
  7158. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  7159. Error(Basic.invalidPosition,"problems during offset computation in module");
  7160. END;
  7161. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  7162. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7163. scope.ownerModule.AddScope(scope);
  7164. END;
  7165. phase := prevPhase;
  7166. currentScope := prevScope;
  7167. error := error OR prevError;
  7168. END Declarations;
  7169. (* nopov *)
  7170. (** check if all operators from one module are compatible to the ones in the other module
  7171. - check if there are not multiple operators with the same signature
  7172. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7173. - check for all operators whose signatures are compatible, whether the return types are compatible
  7174. note that:
  7175. - the return type is not considered to be part of the signature
  7176. - two signatures are considered compatible, if all of the operands are compatible
  7177. **)
  7178. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7179. VAR
  7180. thisOperator, thatOperator: SyntaxTree.Operator;
  7181. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7182. thisParameter, thatParameter: SyntaxTree.Parameter;
  7183. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7184. i: LONGINT;
  7185. BEGIN
  7186. currentScope := thisModuleScope;
  7187. hasError := FALSE;
  7188. (* go through all operators in the other module *)
  7189. thatOperator := thatModuleScope.firstOperator;
  7190. WHILE (thatOperator # NIL) & ~hasError DO
  7191. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7192. (* the other operator is accessible *)
  7193. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7194. (* the other operator is not the conversion operator *)
  7195. (* go through all operators in this module *)
  7196. thisOperator := thisModuleScope.firstOperator;
  7197. WHILE (thisOperator # NIL) & ~hasError DO
  7198. IF thisOperator # thatOperator THEN
  7199. (* the operators are not the same *)
  7200. IF thisOperator.name = thatOperator.name THEN
  7201. (* the operators share the same identifier *)
  7202. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7203. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7204. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7205. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7206. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7207. (* both operators have the same paramter count *)
  7208. thisParameter := thisProcedureType.firstParameter;
  7209. thatParameter := thatProcedureType.firstParameter;
  7210. operandsAreEqual := TRUE;
  7211. operandsAreCompatible := TRUE;
  7212. (* go through all parameters *)
  7213. FOR i := 1 TO thisProcedureType.numberParameters DO
  7214. ASSERT(thatParameter # NIL);
  7215. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7216. operandsAreEqual := FALSE;
  7217. IF TypeDistance(system, thisParameter.type, thatParameter.type, thisParameter.kind = SyntaxTree.VarParameter) =Infinity THEN
  7218. operandsAreCompatible := FALSE
  7219. END
  7220. END;
  7221. thisParameter := thisParameter.nextParameter;
  7222. thatParameter := thatParameter.nextParameter
  7223. END;
  7224. IF operandsAreEqual THEN
  7225. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  7226. hasError := TRUE
  7227. ELSIF operandsAreCompatible THEN
  7228. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7229. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  7230. hasError := TRUE
  7231. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7232. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7233. hasError := TRUE
  7234. END
  7235. END
  7236. END
  7237. END
  7238. END;
  7239. thisOperator := thisOperator.nextOperator
  7240. END
  7241. END
  7242. END;
  7243. thatOperator := thatOperator.nextOperator
  7244. END
  7245. END CheckInterOperatorConformity;
  7246. (** check module:
  7247. - check module declaration
  7248. - add context, if necessary
  7249. - remove module from import cache, if necessary
  7250. - check declarations
  7251. - resolve all type fixes
  7252. - check implementation (bodies)
  7253. **)
  7254. PROCEDURE Module*(x: SyntaxTree.Module);
  7255. VAR (* nopov *)
  7256. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7257. BEGIN
  7258. prevScope := currentScope;
  7259. prevIsCellNet := currentIsCellNet;
  7260. module := x;
  7261. ASSERT(x # NIL);
  7262. global := system.globalScope[x.case];
  7263. x.moduleScope.SetGlobalScope(global);
  7264. currentScope := global;
  7265. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  7266. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7267. RemoveModuleFromCache(importCache,x);
  7268. Declarations(x.moduleScope, FALSE, {0,1});
  7269. FixTypes();
  7270. IF module.isCellNet THEN
  7271. currentIsCellNet := TRUE;
  7272. modifier := x.modifiers;
  7273. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7274. CheckModifiers(modifier, FALSE);
  7275. END;
  7276. (* nopov *)
  7277. IF ~error THEN
  7278. (* check if operators conform to each other within this module *)
  7279. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7280. (* go through all imports *)
  7281. import := x.moduleScope.firstImport;
  7282. WHILE import # NIL DO
  7283. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7284. (* check if all operators in this module conform to the ones of the imported module *)
  7285. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7286. END;
  7287. import := import.nextImport
  7288. END;
  7289. END;
  7290. Implementations(x);
  7291. module := NIL;
  7292. currentIsCellNet := prevIsCellNet;
  7293. currentScope := prevScope;
  7294. END Module;
  7295. END Checker;
  7296. Warnings*=OBJECT
  7297. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7298. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7299. BEGIN
  7300. SELF.diagnostics := diagnostics
  7301. END InitWarnings;
  7302. (** types *)
  7303. PROCEDURE Type(CONST x: SyntaxTree.Type);
  7304. BEGIN
  7305. IF SyntaxTree.Warned IN x.state THEN RETURN END;
  7306. x.SetState(SyntaxTree.Warned);
  7307. WITH x:
  7308. SyntaxTree.ArrayType DO Type(x.arrayBase);
  7309. |SyntaxTree.MathArrayType DO Type(x.arrayBase);
  7310. |SyntaxTree.PointerType DO Type(x.pointerBase);
  7311. |SyntaxTree.RecordType DO Scope(x.recordScope);
  7312. |SyntaxTree.CellType DO Scope(x.cellScope)
  7313. ELSE
  7314. END;
  7315. END Type;
  7316. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7317. VAR msg: ARRAY 256 OF CHAR;
  7318. BEGIN
  7319. Global.GetSymbolName(x,msg);
  7320. Strings.Append(msg," ");
  7321. Strings.Append(msg,text);
  7322. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  7323. END Warning;
  7324. (** symbols *)
  7325. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7326. BEGIN
  7327. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7328. IF ~(x IS SyntaxTree.Parameter) THEN
  7329. Warning(x,"never used");
  7330. END;
  7331. END;
  7332. WITH x:
  7333. SyntaxTree.Procedure DO
  7334. Scope(x.procedureScope)
  7335. | SyntaxTree.TypeDeclaration DO
  7336. Type(x.declaredType);
  7337. ELSE
  7338. END;
  7339. END Symbol;
  7340. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7341. VAR
  7342. symbol: SyntaxTree.Symbol;
  7343. BEGIN
  7344. symbol := scope.firstSymbol;
  7345. WHILE(symbol # NIL) DO
  7346. Symbol(symbol);
  7347. symbol := symbol.nextSymbol;
  7348. END;
  7349. END Scope;
  7350. PROCEDURE Module*(x: SyntaxTree.Module);
  7351. BEGIN
  7352. SELF.module := x;
  7353. Scope(x.moduleScope);
  7354. END Module;
  7355. END Warnings;
  7356. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7357. BEGIN
  7358. RETURN procedure.isOberonInline OR procedure.isInline &
  7359. ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7360. END IsOberonInline;
  7361. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7362. BEGIN
  7363. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7364. END Resolved;
  7365. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  7366. VAR i: LONGINT;
  7367. BEGIN
  7368. i := 1;
  7369. WHILE i < x DO
  7370. i := i *2
  7371. END;
  7372. RETURN i=x
  7373. END PowerOf2;
  7374. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7375. BEGIN
  7376. RETURN
  7377. (scope # NIL) &
  7378. (scope IS SyntaxTree.ModuleScope)
  7379. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7380. OR
  7381. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7382. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7383. END IsCellNetScope;
  7384. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7385. BEGIN
  7386. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7387. END IsCellScope;
  7388. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7389. BEGIN
  7390. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7391. RETURN (scope # NIL) & IsCellNetScope(scope)
  7392. END InCellNetScope;
  7393. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  7394. BEGIN
  7395. ASSERT(size MOD system.dataUnit = 0);
  7396. RETURN size DIV system.dataUnit
  7397. END ToMemoryUnits;
  7398. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  7399. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  7400. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  7401. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  7402. BEGIN
  7403. IF t = NIL THEN
  7404. RETURN TRUE
  7405. ELSE
  7406. t := t.resolved;
  7407. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  7408. END;
  7409. END TypeAllowed;
  7410. BEGIN
  7411. type := type.resolved;
  7412. IF ~(type IS SyntaxTree.ProcedureType) THEN
  7413. RETURN FALSE
  7414. ELSE
  7415. procedureType := type(SyntaxTree.ProcedureType);
  7416. numberParameters := procedureType.numberParameters;
  7417. RETURN
  7418. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  7419. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  7420. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  7421. END;
  7422. END GetProcedureAllowed;
  7423. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  7424. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  7425. VAR import: SyntaxTree.Import;
  7426. BEGIN
  7427. import := importCache.ImportByModuleName(x.name,x.context);
  7428. IF import # NIL THEN
  7429. importCache.RemoveImporters(x.name,x.context);
  7430. END;
  7431. END RemoveModuleFromCache;
  7432. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  7433. (* to <- this assignment compatibility *)
  7434. VAR result: BOOLEAN;
  7435. BEGIN
  7436. IF this= NIL THEN result := (to=NIL)
  7437. ELSIF to=NIL THEN result := FALSE
  7438. ELSE
  7439. (*! will be replaced by this:
  7440. ELSE result := this.CompatibleTo(to.resolved);
  7441. *)
  7442. this := this.resolved; to := to.resolved;
  7443. IF to=SyntaxTree.invalidType THEN result := FALSE
  7444. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  7445. ELSIF (to = this) OR (to.SameType(this)) THEN
  7446. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  7447. ELSIF to IS SyntaxTree.BasicType THEN
  7448. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  7449. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  7450. result := this.CompatibleTo(to.resolved)
  7451. ELSE
  7452. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  7453. END
  7454. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  7455. result := to.sizeInBits >= this.sizeInBits;
  7456. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  7457. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  7458. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  7459. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  7460. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  7461. result := TRUE;
  7462. ELSIF to IS SyntaxTree.AnyType THEN
  7463. 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);
  7464. ELSIF to IS SyntaxTree.ObjectType THEN
  7465. 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 *) ;
  7466. ELSIF to IS SyntaxTree.ByteType THEN
  7467. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  7468. ELSIF to IS SyntaxTree.CharacterType THEN
  7469. result := IsCharacterType(this)
  7470. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  7471. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  7472. 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)
  7473. OR IsTensor(this)
  7474. ) THEN
  7475. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  7476. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  7477. result := TRUE;
  7478. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  7479. result := TRUE;
  7480. ELSE
  7481. result := FALSE
  7482. END;
  7483. ELSIF IsUnsafePointer(to) & IsUnsafePointer(this) THEN
  7484. result := TRUE;
  7485. ELSIF to IS SyntaxTree.PointerType THEN
  7486. result := (this IS SyntaxTree.NilType) OR
  7487. IsUnsafePointer(to) & ( (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR IsPointerType(this) OR IsTensor(this)) OR
  7488. (IsPointerType(this) & IsTypeExtension(to,this) OR
  7489. ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this))) & (~to.isRealtime OR this.isRealtime);
  7490. ELSIF to IS SyntaxTree.ProcedureType THEN
  7491. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  7492. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  7493. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  7494. ELSIF to IS SyntaxTree.RecordType THEN
  7495. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  7496. ELSIF to IS SyntaxTree.ArrayType THEN
  7497. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  7498. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  7499. ELSIF StaticArrayCompatible(to, this) THEN
  7500. result := TRUE
  7501. ELSE
  7502. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  7503. END;
  7504. ELSIF to IS SyntaxTree.MathArrayType THEN
  7505. IF this IS SyntaxTree.MathArrayType THEN
  7506. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  7507. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7508. result := TRUE;
  7509. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  7510. result := TRUE;
  7511. ELSE
  7512. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  7513. END;
  7514. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  7515. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  7516. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  7517. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  7518. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  7519. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  7520. (* ARRAY [x] OF <- ARRAY [x] OF *)
  7521. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  7522. ELSE
  7523. result := FALSE
  7524. END;
  7525. ELSE
  7526. result := FALSE;
  7527. END;
  7528. ELSIF to IS SyntaxTree.StringType THEN
  7529. result := FALSE;
  7530. ELSIF to IS SyntaxTree.EnumerationType THEN
  7531. result := IsEnumerationExtension(this,to);
  7532. ELSIF to IS SyntaxTree.PortType THEN
  7533. result := SameType(to, this)
  7534. ELSE
  7535. Printout.Info("CompatibleTo",to);
  7536. HALT(100); (* implement missing type check *)
  7537. END;
  7538. END;
  7539. RETURN result
  7540. END CompatibleTo;
  7541. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  7542. VAR actualBase, formalBase: SyntaxTree.Type;
  7543. BEGIN
  7544. IF SameType(formal,actual) THEN
  7545. RETURN TRUE
  7546. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  7547. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  7548. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  7549. RETURN
  7550. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7551. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  7552. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  7553. & StaticArrayCompatible(formalBase,actualBase)
  7554. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  7555. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  7556. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7557. RETURN
  7558. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  7559. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7560. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  7561. & StaticArrayCompatible(formalBase,actualBase)
  7562. ELSE RETURN FALSE
  7563. END;
  7564. END StaticArrayCompatible;
  7565. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  7566. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  7567. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  7568. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  7569. BEGIN
  7570. result := SameType(formal,actual);
  7571. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  7572. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  7573. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7574. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  7575. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  7576. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  7577. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7578. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7579. & TC(formalBase, actualBase);
  7580. END;
  7581. RETURN result
  7582. END TC;
  7583. BEGIN
  7584. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  7585. ELSE
  7586. arrayBase := formalType.arrayBase.resolved;
  7587. IF (actualType IS SyntaxTree.StringType) THEN
  7588. result := arrayBase IS SyntaxTree.CharacterType
  7589. ELSIF actualType IS SyntaxTree.ArrayType THEN
  7590. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  7591. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  7592. result := TC(formalType, actualType);
  7593. ELSE
  7594. result := (arrayBase IS SyntaxTree.ByteType)
  7595. END;
  7596. END;
  7597. RETURN result
  7598. END OpenArrayCompatible;
  7599. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  7600. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  7601. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  7602. BEGIN
  7603. IF actualType IS SyntaxTree.MathArrayType THEN
  7604. actualArray := actualType(SyntaxTree.MathArrayType);
  7605. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  7606. (*
  7607. ARRAY [?] OF -> ARRAY [?|*|k] OF
  7608. ARRAY [?|*|k] OF -> ARRAY [?] OF
  7609. *)
  7610. actualBase := ArrayBase(actualType,Infinity);
  7611. formalBase := ArrayBase(formalType,Infinity);
  7612. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  7613. ELSE
  7614. (*
  7615. ARRAY [*|k] OF -> ARRAY [*|n] OF
  7616. *)
  7617. formalBase := Resolved(formalType.arrayBase);
  7618. actualBase := Resolved(actualArray.arrayBase);
  7619. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  7620. (*
  7621. ARRAY [k] -> ARRAY [n]
  7622. *)
  7623. result := (formalType.staticLength = actualArray.staticLength)
  7624. ELSE
  7625. result := TRUE
  7626. END;
  7627. IF ~result THEN
  7628. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  7629. ELSIF actualBase = NIL THEN result := FALSE
  7630. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  7631. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  7632. ELSE
  7633. result := SameType(formalBase,actualBase)
  7634. END;
  7635. END;
  7636. ELSE
  7637. result := FALSE
  7638. END;
  7639. RETURN result
  7640. END MathArrayCompatible;
  7641. (**
  7642. Math Array Type distance for assignments / parameter passings of the form
  7643. from -> to
  7644. variants:
  7645. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  7646. allowed:
  7647. static -> static (& size match)
  7648. static -> open
  7649. static -> tensor
  7650. open -> open
  7651. open -> tensor
  7652. open -> static
  7653. tensor -> tensor
  7654. tensor -> open
  7655. tensor -> static
  7656. **)
  7657. (*! think about the metric here: is form matching more important than element type matching? *)
  7658. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  7659. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  7660. BEGIN
  7661. fromBase := Resolved(from.arrayBase);
  7662. toBase := Resolved(to.arrayBase);
  7663. i := Infinity;
  7664. IF (from = to) OR (from.SameType(to)) THEN
  7665. i := 0;
  7666. ELSIF (from.form = to.form) THEN
  7667. (* static -> static, open -> open, tensor -> tensor *)
  7668. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  7669. IF fromBase = toBase THEN i := 0
  7670. ELSIF toBase = NIL THEN i := 1
  7671. ELSIF fromBase = NIL THEN i := Infinity;
  7672. ELSIF toBase.SameType(fromBase) THEN i := 0
  7673. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  7674. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  7675. ELSE
  7676. i := TypeDistance(system,fromBase, toBase, varpar);
  7677. IF i < Infinity THEN i := i * 5 END;
  7678. END;
  7679. END;
  7680. ELSIF (to.form = SyntaxTree.Static) THEN
  7681. (* forbidden *)
  7682. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  7683. (* static -> tensor, open -> tensor, tensor -> open *)
  7684. IF (toBase=fromBase) THEN i := 0;
  7685. ELSIF toBase = NIL THEN i := 1;
  7686. ELSIF toBase.SameType(fromBase) THEN i := 0
  7687. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  7688. toBase := ArrayBase(toBase,Infinity);
  7689. IF (fromBase=toBase) THEN i := 0
  7690. ELSIF (toBase = NIL) THEN i:= 1
  7691. ELSIF (fromBase = NIL) THEN i := Infinity;
  7692. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  7693. IF i < Infinity THEN i := i * 5 END;
  7694. END;
  7695. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  7696. fromBase := ArrayBase(fromBase,Infinity);
  7697. IF (fromBase=toBase) THEN i := 0
  7698. ELSIF (toBase = NIL) THEN i := 1
  7699. ELSIF (fromBase = NIL) THEN i := Infinity;
  7700. ELSIF toBase.SameType(fromBase) THEN i := 0
  7701. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  7702. IF i < Infinity THEN i := i * 5 END;
  7703. END;
  7704. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  7705. IF i < Infinity THEN i := i * 5 END;
  7706. END;
  7707. IF i # Infinity THEN INC(i,2) END;
  7708. ELSIF (from.form = SyntaxTree.Static) THEN
  7709. (* static -> open *)
  7710. IF (toBase=fromBase) THEN i := 0;
  7711. ELSIF toBase = NIL THEN i := 1
  7712. ELSIF fromBase = NIL THEN i := Infinity
  7713. ELSIF toBase.SameType(fromBase) THEN i := 0
  7714. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  7715. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  7716. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  7717. IF i < Infinity THEN i := i * 5 END;
  7718. END;
  7719. IF i # Infinity THEN INC(i,1) END;
  7720. ELSE HALT(100); (* unknown case *)
  7721. END;
  7722. RETURN i;
  7723. END MathArrayTypeDistance;
  7724. (** compute and return the distance of two array types
  7725. - return the distance of the base types
  7726. **)
  7727. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  7728. VAR i: LONGINT;
  7729. BEGIN
  7730. i := Infinity;
  7731. IF from = to THEN
  7732. i := 0
  7733. ELSE
  7734. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  7735. (*
  7736. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  7737. i := TypeDistance(from.base, to.base);
  7738. IF i >= 0 THEN INC(i) END
  7739. ELSIF (from.mode = open) & (to.mode = open) THEN
  7740. i := TypeDistance(from.base, to.base);
  7741. *)
  7742. END;
  7743. RETURN i
  7744. END ArrayTypeDistance;
  7745. (** compute the signature distance of a procedure and an actual parameter list
  7746. - if any of the parameters are not compatible, the result is infinite
  7747. - add up and return the distance over all parameters
  7748. **)
  7749. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  7750. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  7751. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  7752. BEGIN
  7753. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  7754. result := Infinity
  7755. ELSE
  7756. formalParameter := procedureType.firstParameter;
  7757. i := 0;
  7758. result := 0;
  7759. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  7760. WHILE (formalParameter # NIL) & (result # Infinity) DO
  7761. actualParameter := actualParameters.GetExpression(i);
  7762. ASSERT(formalParameter.type # NIL);
  7763. IF (actualParameter.type = NIL) THEN distance := Infinity
  7764. ELSE
  7765. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  7766. END;
  7767. IF distance = Infinity THEN
  7768. result := Infinity;
  7769. ELSE
  7770. to := formalParameter.type.resolved;
  7771. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  7772. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  7773. (* already handled varpar *)
  7774. (*
  7775. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  7776. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  7777. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  7778. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  7779. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  7780. END;
  7781. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  7782. result := Infinity
  7783. END;
  7784. *)
  7785. INC(result, distance);
  7786. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  7787. INC(result, distance);
  7788. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  7789. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  7790. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  7791. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  7792. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  7793. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  7794. END;
  7795. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  7796. result := Infinity
  7797. END;
  7798. ELSE
  7799. result := Infinity
  7800. END;
  7801. ELSE
  7802. INC(result,distance);
  7803. END;
  7804. END;
  7805. (*
  7806. Printout.Info("actual=", actualParameter);
  7807. Printout.Info("formal=", formalParameter);
  7808. TRACE(result);
  7809. *)
  7810. formalParameter := formalParameter.nextParameter; INC(i);
  7811. END;
  7812. END;
  7813. ASSERT(result >= 0);
  7814. RETURN result
  7815. END Distance;
  7816. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  7817. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  7818. BEGIN
  7819. IF right.numberParameters # (procedureType.numberParameters) THEN
  7820. result := Infinity
  7821. ELSE
  7822. formalParameter := procedureType.firstParameter;
  7823. rightParameter := right.firstParameter;
  7824. i := 0;
  7825. result := 0;
  7826. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  7827. WHILE (formalParameter # NIL) & (result # Infinity) DO
  7828. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  7829. IF distance = Infinity THEN
  7830. result := Infinity;
  7831. ELSE
  7832. INC(result,distance);
  7833. END;
  7834. formalParameter := formalParameter.nextParameter;
  7835. rightParameter := rightParameter.nextParameter;
  7836. END;
  7837. END;
  7838. ASSERT(result >= 0);
  7839. RETURN result
  7840. END ProcedureTypeDistance;
  7841. (** compute and return the distance between two types, used for computation of signature distance
  7842. from -> to
  7843. **)
  7844. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  7845. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  7846. BEGIN
  7847. i := Infinity;
  7848. IF from = to THEN
  7849. i := 0
  7850. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  7851. ELSIF to.SameType(from) THEN
  7852. i := 0;
  7853. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  7854. i := Infinity;
  7855. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  7856. i := 10;
  7857. ELSIF (from IS SyntaxTree.StringType) THEN
  7858. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  7859. ELSIF (from IS SyntaxTree.CharacterType) THEN
  7860. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  7861. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  7862. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  7863. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  7864. i := 1
  7865. ELSIF (from IS SyntaxTree.NilType) THEN
  7866. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  7867. (*
  7868. ELSIF (from = NoType) THEN
  7869. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  7870. *)
  7871. ELSIF (from IS SyntaxTree.BasicType) THEN
  7872. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  7873. IF varpar & (i # 0) THEN i := Infinity END;
  7874. ELSIF (from IS SyntaxTree.ArrayType) THEN
  7875. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  7876. ELSIF (from IS SyntaxTree.RecordType) THEN
  7877. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  7878. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  7879. IF to IS SyntaxTree.MathArrayType THEN
  7880. (*
  7881. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  7882. i := Infinity;
  7883. ELSE
  7884. *)
  7885. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  7886. (*
  7887. END;
  7888. *)
  7889. END
  7890. ELSIF (from IS SyntaxTree.PointerType) THEN
  7891. ptr := from(SyntaxTree.PointerType);
  7892. IF (to IS SyntaxTree.AnyType) THEN i := 1
  7893. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  7894. (* ELSE i := TypeDistance(ptr.base, to); *)
  7895. END
  7896. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  7897. IF (to IS SyntaxTree.ProcedureType) THEN
  7898. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  7899. END;
  7900. ELSIF (from IS SyntaxTree.PortType) THEN
  7901. IF (to IS SyntaxTree.PortType) THEN
  7902. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  7903. i := 0;
  7904. END;
  7905. END;
  7906. (*no procedure test, procedure must be the same*)
  7907. END;
  7908. RETURN i
  7909. END TypeDistance;
  7910. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  7911. BEGIN
  7912. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  7913. END IsIntegerType;
  7914. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  7915. BEGIN
  7916. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  7917. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  7918. OR IsPointerType(type)
  7919. )
  7920. END IsAddressType;
  7921. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  7922. BEGIN
  7923. RETURN (type # NIL) & ((type.resolved IS SyntaxTree.IntegerType) & (type.resolved(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type.resolved IS SyntaxTree.SizeType))
  7924. END IsSizeType;
  7925. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  7926. BEGIN
  7927. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  7928. END IsSignedIntegerType;
  7929. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  7930. BEGIN
  7931. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  7932. END IsUnsignedIntegerType;
  7933. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  7934. VAR result: BOOLEAN;
  7935. BEGIN
  7936. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  7937. value := x.resolved(SyntaxTree.IntegerValue).value;
  7938. result := TRUE
  7939. ELSE
  7940. result := FALSE
  7941. END;
  7942. RETURN result
  7943. END IsIntegerValue;
  7944. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  7945. VAR result: BOOLEAN;
  7946. BEGIN
  7947. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  7948. value := x.resolved(SyntaxTree.EnumerationValue).value;
  7949. result := TRUE
  7950. ELSE
  7951. result := FALSE
  7952. END;
  7953. RETURN result
  7954. END IsEnumerationValue;
  7955. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  7956. VAR result: BOOLEAN;
  7957. BEGIN
  7958. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  7959. value := x.resolved(SyntaxTree.RealValue).value;
  7960. result := TRUE
  7961. ELSE
  7962. result := FALSE
  7963. END;
  7964. RETURN result
  7965. END IsRealValue;
  7966. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  7967. VAR result: BOOLEAN;
  7968. BEGIN
  7969. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  7970. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  7971. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  7972. result := TRUE
  7973. ELSE
  7974. result := FALSE
  7975. END;
  7976. RETURN result
  7977. END IsComplexValue;
  7978. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  7979. VAR result: BOOLEAN;
  7980. BEGIN
  7981. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  7982. value := x.resolved(SyntaxTree.CharacterValue).value;
  7983. result := TRUE
  7984. ELSE
  7985. result := FALSE
  7986. END;
  7987. RETURN result
  7988. END IsCharacterValue;
  7989. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  7990. VAR result: BOOLEAN;
  7991. BEGIN
  7992. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  7993. value := x.resolved(SyntaxTree.BooleanValue).value;
  7994. result := TRUE
  7995. ELSE
  7996. result := FALSE
  7997. END;
  7998. RETURN result
  7999. END IsBooleanValue;
  8000. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8001. VAR result: BOOLEAN;
  8002. BEGIN
  8003. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8004. value := x.resolved(SyntaxTree.SetValue).value;
  8005. result := TRUE
  8006. ELSE
  8007. result := FALSE
  8008. END;
  8009. RETURN result
  8010. END IsSetValue;
  8011. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8012. VAR result: BOOLEAN;
  8013. BEGIN
  8014. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8015. value := x.resolved(SyntaxTree.StringValue).value;
  8016. result := TRUE
  8017. ELSE
  8018. result := FALSE
  8019. END;
  8020. RETURN result
  8021. END IsStringValue;
  8022. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8023. BEGIN
  8024. x := x.resolved;
  8025. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8026. END Indexable;
  8027. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8028. BEGIN
  8029. RETURN t1.SameType(t2.resolved);
  8030. END SameType;
  8031. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8032. BEGIN
  8033. IF t IS SyntaxTree.MathArrayType THEN
  8034. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8035. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8036. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8037. DEC(max);
  8038. END;
  8039. ELSIF t IS SyntaxTree.ArrayType THEN
  8040. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8041. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8042. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8043. END;
  8044. END;
  8045. RETURN t;
  8046. END ArrayBase;
  8047. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8048. BEGIN
  8049. type := type.resolved;
  8050. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8051. base := type(SyntaxTree.ArrayType).arrayBase;
  8052. RETURN TRUE;
  8053. END;
  8054. RETURN FALSE;
  8055. END IsOpenArray;
  8056. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8057. BEGIN
  8058. type := type.resolved;
  8059. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8060. base := type(SyntaxTree.ArrayType).arrayBase;
  8061. dim := type(SyntaxTree.ArrayType).staticLength;
  8062. RETURN TRUE
  8063. ELSE
  8064. RETURN FALSE
  8065. END;
  8066. END IsStaticArray;
  8067. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8068. BEGIN
  8069. type := type.resolved;
  8070. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8071. base := type(SyntaxTree.ArrayType).arrayBase;
  8072. RETURN TRUE
  8073. ELSE
  8074. RETURN FALSE
  8075. END;
  8076. END IsDynamicArray;
  8077. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8078. VAR i: LONGINT;
  8079. BEGIN
  8080. i := 0;
  8081. t := t.resolved;
  8082. IF t IS SyntaxTree.MathArrayType THEN
  8083. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8084. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8085. END;
  8086. ELSIF t IS SyntaxTree.ArrayType THEN
  8087. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8088. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8089. END;
  8090. END;
  8091. RETURN i
  8092. END Dimension;
  8093. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8094. BEGIN
  8095. RETURN expression.assignable;
  8096. END IsVariable;
  8097. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8098. BEGIN
  8099. IF (symbol IS SyntaxTree.Parameter) THEN
  8100. WITH symbol: SyntaxTree.Parameter DO
  8101. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8102. END;
  8103. ELSE
  8104. RETURN FALSE
  8105. END;
  8106. END IsVariableParameter;
  8107. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8108. VAR result: BOOLEAN;
  8109. BEGIN
  8110. IF type = NIL THEN result := FALSE
  8111. ELSE
  8112. type := type.resolved;
  8113. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8114. END;
  8115. RETURN result
  8116. END IsPointerType;
  8117. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8118. VAR result: BOOLEAN;
  8119. BEGIN
  8120. IF type = NIL THEN result := FALSE
  8121. ELSE
  8122. type := type.resolved;
  8123. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe OR
  8124. (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  8125. & type(SyntaxTree.MathArrayType).isUnsafe;
  8126. END;
  8127. RETURN result
  8128. END IsUnsafePointer;
  8129. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8130. BEGIN
  8131. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8132. END IsDisposable;
  8133. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8134. VAR result: BOOLEAN;
  8135. BEGIN
  8136. IF type = NIL THEN result := FALSE
  8137. ELSE
  8138. type := type.resolved;
  8139. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8140. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8141. result := result OR (type IS SyntaxTree.ObjectType);
  8142. END;
  8143. RETURN result
  8144. END IsPointerToRecord;
  8145. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8146. VAR result: BOOLEAN;
  8147. BEGIN
  8148. IF type = NIL THEN result := FALSE
  8149. ELSE
  8150. type := type.resolved;
  8151. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8152. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8153. ;
  8154. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8155. result := result OR (type IS SyntaxTree.ObjectType);
  8156. END;
  8157. RETURN result
  8158. END IsPointerToObject;
  8159. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8160. BEGIN
  8161. IF type # NIL THEN
  8162. RETURN type.resolved.hasPointers
  8163. ELSE
  8164. RETURN FALSE
  8165. END;
  8166. END ContainsPointer;
  8167. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8168. BEGIN
  8169. IF type = NIL THEN RETURN FALSE END;
  8170. type := type.resolved;
  8171. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8172. END IsStringType;
  8173. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8174. BEGIN
  8175. IF type = NIL THEN RETURN FALSE END;
  8176. type := type.resolved;
  8177. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8178. END IsCharacterType;
  8179. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8180. BEGIN
  8181. IF type = NIL THEN RETURN FALSE END;
  8182. type := type.resolved;
  8183. RETURN (type IS SyntaxTree.EnumerationType)
  8184. END IsEnumerationType;
  8185. (** cf. section "Type extension (base type)" in the language report **)
  8186. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8187. VAR result: BOOLEAN;
  8188. BEGIN
  8189. ASSERT(base # NIL); ASSERT(extension # NIL);
  8190. base := base.resolved; extension := extension.resolved;
  8191. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8192. result := TRUE;
  8193. ELSE
  8194. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8195. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8196. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8197. END;
  8198. WHILE (extension # NIL) & (extension # base) DO
  8199. IF extension IS SyntaxTree.RecordType THEN
  8200. extension := extension(SyntaxTree.RecordType).baseType;
  8201. IF (extension # NIL) THEN extension := extension.resolved END;
  8202. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8203. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8204. END;
  8205. ELSE extension := NIL;
  8206. END;
  8207. END;
  8208. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8209. END;
  8210. RETURN result
  8211. END IsTypeExtension;
  8212. (** check if base is the base enumeration type of extension **)
  8213. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8214. BEGIN
  8215. base := base.resolved; extension := extension.resolved;
  8216. WHILE (extension # NIL) & (extension # base) DO
  8217. IF extension IS SyntaxTree.EnumerationType THEN
  8218. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8219. IF extension # NIL THEN extension := extension.resolved END;
  8220. ELSE
  8221. extension := NIL
  8222. END;
  8223. END;
  8224. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8225. END IsEnumerationExtension;
  8226. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8227. BEGIN
  8228. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8229. RETURN TRUE
  8230. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8231. RETURN TRUE
  8232. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8233. RETURN TRUE
  8234. ELSE
  8235. RETURN FALSE
  8236. END
  8237. END IsCallable;
  8238. (** compute and return the distance of two record types
  8239. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8240. **)
  8241. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8242. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8243. BEGIN
  8244. i := 0;
  8245. WHILE (from # NIL) & (from # to) DO
  8246. baseType := from.baseType;
  8247. IF (baseType # NIL) THEN
  8248. baseType := baseType.resolved;
  8249. IF baseType IS SyntaxTree.PointerType THEN
  8250. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8251. END;
  8252. IF baseType IS SyntaxTree.RecordType THEN
  8253. from := baseType(SyntaxTree.RecordType);
  8254. ELSE
  8255. from := NIL;
  8256. END;
  8257. ELSE
  8258. from := NIL
  8259. END;
  8260. INC(i)
  8261. END;
  8262. IF from = NIL THEN i := Infinity END;
  8263. RETURN i
  8264. END RecordTypeDistance;
  8265. (** compute and return the distance of two pointer types **)
  8266. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8267. BEGIN
  8268. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8269. RETURN Infinity;
  8270. ELSE
  8271. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8272. END;
  8273. END PointerTypeDistance;
  8274. (** check if expression contains a symbol designator pointing to a type declaration.
  8275. - if so then enter type declaration into typeDeclaration and return true else return false
  8276. **)
  8277. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8278. VAR result: BOOLEAN;
  8279. BEGIN
  8280. result := FALSE;
  8281. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8282. result := TRUE;
  8283. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8284. END;
  8285. RETURN result
  8286. END IsTypeDesignator;
  8287. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8288. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8289. VAR result: BOOLEAN;
  8290. BEGIN
  8291. type := type.resolved;
  8292. IF type IS SyntaxTree.PointerType THEN
  8293. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8294. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8295. result := TRUE
  8296. ELSE
  8297. result := type IS SyntaxTree.RecordType
  8298. END;
  8299. RETURN result
  8300. END IsExtensibleType;
  8301. PROCEDURE IsIndexOperator*(expression: SyntaxTree.Expression): BOOLEAN;
  8302. VAR left: SyntaxTree.Expression; symbol: SyntaxTree.Symbol;
  8303. BEGIN
  8304. WITH expression: SyntaxTree.ProcedureCallDesignator DO
  8305. left := expression.left;
  8306. WITH left: SyntaxTree.SymbolDesignator DO
  8307. symbol := left.symbol;
  8308. WITH symbol: SyntaxTree.Operator DO
  8309. RETURN symbol.name = "[]";
  8310. ELSE
  8311. RETURN FALSE
  8312. END;
  8313. ELSE
  8314. RETURN FALSE
  8315. END;
  8316. ELSE
  8317. RETURN FALSE
  8318. END;
  8319. END IsIndexOperator;
  8320. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8321. BEGIN
  8322. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8323. (d IS SyntaxTree.SymbolDesignator) &
  8324. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8325. OR
  8326. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8327. END IsUnextensibleRecord;
  8328. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8329. BEGIN
  8330. IF IsUnextensibleRecord(d) THEN
  8331. RETURN FALSE
  8332. ELSE RETURN IsExtensibleType(d.type.resolved)
  8333. END;
  8334. END IsExtensibleDesignator;
  8335. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8336. BEGIN
  8337. type := type.resolved;
  8338. IF (type IS SyntaxTree.PointerType) THEN
  8339. RETURN TRUE
  8340. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8341. RETURN TRUE
  8342. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8343. RETURN TRUE
  8344. ELSIF (type IS SyntaxTree.BasicType) THEN
  8345. RETURN TRUE
  8346. END;
  8347. RETURN FALSE
  8348. END IsBasicType;
  8349. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8350. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8351. BEGIN
  8352. baseType := record.baseType;
  8353. IF (baseType # NIL) THEN
  8354. baseType := baseType.resolved;
  8355. IF (baseType IS SyntaxTree.PointerType) THEN
  8356. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8357. END;
  8358. END;
  8359. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8360. recordType := baseType(SyntaxTree.RecordType);
  8361. ELSE
  8362. recordType := NIL;
  8363. END;
  8364. RETURN recordType
  8365. END RecordBase;
  8366. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8367. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8368. BEGIN
  8369. baseRecord := RecordBase(scope.ownerRecord);
  8370. IF baseRecord = NIL THEN RETURN NIL END;
  8371. scope := baseRecord.recordScope;
  8372. procedureType := procedure.type.resolved;
  8373. IF procedure IS SyntaxTree.Operator THEN
  8374. operator := scope.firstOperator;
  8375. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8376. (*
  8377. Printout.Info("not same ",procedureType);
  8378. Printout.Info("with ",operator.type);
  8379. *)
  8380. operator := operator.nextOperator;
  8381. END;
  8382. super := operator;
  8383. ELSE
  8384. super := scope.firstProcedure;
  8385. WHILE (super # NIL) & (super.name # procedure.name) DO
  8386. super := super.nextProcedure;
  8387. END;
  8388. END;
  8389. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8390. RETURN super
  8391. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8392. RETURN super
  8393. ELSE
  8394. RETURN FindSuperProcedure(scope,procedure);
  8395. END;
  8396. END FindSuperProcedure;
  8397. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8398. VAR procedure: SyntaxTree.Procedure;
  8399. BEGIN
  8400. procedure := record.recordScope.constructor;
  8401. IF procedure = NIL THEN
  8402. record := RecordBase(record);
  8403. IF record # NIL THEN
  8404. procedure := GetConstructor(record)
  8405. END;
  8406. END;
  8407. RETURN procedure;
  8408. END GetConstructor;
  8409. (* enter a case into a list of cases in a sorted way and check for collision *)
  8410. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  8411. VAR prev,this,new: SyntaxTree.CaseConstant;
  8412. BEGIN
  8413. this := root;
  8414. prev := NIL;
  8415. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8416. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8417. RETURN FALSE
  8418. ELSE
  8419. IF (this # NIL) & (this.min = max+1) THEN
  8420. this.min := min
  8421. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8422. prev.max := min
  8423. ELSE
  8424. NEW(new); new.min := min; new.max := max;
  8425. new.next := this;
  8426. IF prev = NIL THEN
  8427. root := new;
  8428. ELSE
  8429. prev.next := new
  8430. END
  8431. END;
  8432. RETURN TRUE
  8433. END;
  8434. END EnterCase;
  8435. (** generate and return a new checker object, errors are entered into diagnostics **)
  8436. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  8437. VAR checker: Checker;
  8438. BEGIN
  8439. NEW(checker, diagnostics,verboseErrorMessage,cooperative,system,symbolFileFormat,importCache,backend);
  8440. RETURN checker
  8441. END NewChecker;
  8442. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  8443. VAR warnings: Warnings;
  8444. BEGIN
  8445. NEW(warnings, diagnostics); RETURN warnings;
  8446. END NewWarnings;
  8447. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  8448. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  8449. END IsRangeType;
  8450. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  8451. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  8452. END IsMathArrayType;
  8453. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  8454. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  8455. END IsArrayType;
  8456. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  8457. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  8458. END IsComplexType;
  8459. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  8460. VAR
  8461. result: BOOLEAN;
  8462. rangeExpression: SyntaxTree.RangeExpression;
  8463. BEGIN
  8464. IF x IS SyntaxTree.RangeExpression THEN
  8465. rangeExpression := x(SyntaxTree.RangeExpression);
  8466. result := TRUE;
  8467. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  8468. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  8469. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  8470. ELSE
  8471. result := FALSE
  8472. END;
  8473. RETURN result
  8474. END IsStaticRange;
  8475. (** whether a type is a math array of tensor form **)
  8476. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  8477. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  8478. END IsTensor;
  8479. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  8480. BEGIN
  8481. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  8482. length := type(SyntaxTree.MathArrayType).staticLength;
  8483. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  8484. RETURN TRUE
  8485. ELSE
  8486. RETURN FALSE
  8487. END;
  8488. END IsStaticMathArray;
  8489. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8490. BEGIN
  8491. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  8492. END SymbolHasAddress;
  8493. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  8494. BEGIN
  8495. RETURN
  8496. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  8497. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  8498. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  8499. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  8500. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  8501. ;
  8502. END HasAddress;
  8503. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  8504. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  8505. BEGIN
  8506. IF (e IS SyntaxTree.Designator) THEN
  8507. d := e(SyntaxTree.Designator);
  8508. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  8509. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  8510. e := d.left;
  8511. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  8512. END;
  8513. IF d # NIL THEN
  8514. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  8515. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  8516. END;
  8517. END;
  8518. RETURN FALSE;
  8519. END IsLocalVariable;
  8520. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  8521. BEGIN
  8522. IF procedure.scope IS SyntaxTree.RecordScope THEN
  8523. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  8524. ELSE
  8525. RETURN TRUE
  8526. END;
  8527. END IsStaticProcedure;
  8528. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  8529. CONST OptimizeMethodTable = FALSE;
  8530. BEGIN
  8531. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  8532. END InMethodTable;
  8533. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  8534. BEGIN
  8535. IF type = NIL THEN RETURN FALSE
  8536. ELSE
  8537. type := type.resolved;
  8538. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  8539. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  8540. END
  8541. END ReturnedAsParameter;
  8542. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  8543. BEGIN
  8544. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  8545. END StructuredReturnType;
  8546. END FoxSemanticChecker.
  8547. System.FreeDownTo FoxSemanticChecker ~
  8548. System.FreeDownTo FoxIntermediateBackend ~