FoxSemanticChecker.Mod 379 KB

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