FoxSemanticChecker.Mod 366 KB

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