FoxSemanticChecker.Mod 404 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. cooperative: BOOLEAN;
  66. error-: BOOLEAN;
  67. VerboseErrorMessage: BOOLEAN;
  68. typeFixes, pointerFixes: LateFixList;
  69. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  70. arrayBaseImported: BOOLEAN;
  71. complexNumbersImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. inConversion: LONGINT;
  77. (* temporary variables for the visitors
  78. they replace variables on a stack during use of the visitor pattern and may only be
  79. - set in AcceptXXX procedures
  80. - set and read in ResolveXXX procedures
  81. *)
  82. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  83. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  84. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  85. currentScope-: SyntaxTree.Scope;
  86. currentIsRealtime: BOOLEAN;
  87. currentIsUnreachable: BOOLEAN;
  88. currentIsCellNet: BOOLEAN;
  89. currentIsBodyProcedure: BOOLEAN;
  90. currentIsExclusive: BOOLEAN;
  91. global: SyntaxTree.ModuleScope;
  92. withEntries: WithEntry;
  93. activeCellsStatement: BOOLEAN;
  94. replacements*: Replacement;
  95. cellsAreObjects: BOOLEAN;
  96. variableAccessed: BOOLEAN;
  97. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  98. BEGIN
  99. SELF.diagnostics := diagnostics;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. complexNumbersImported := FALSE;
  114. SELF.VerboseErrorMessage := verboseErrorMessage;
  115. global := NIL;
  116. phase := UndefinedPhase;
  117. currentIsRealtime := FALSE;
  118. currentIsUnreachable := FALSE;
  119. currentIsCellNet := FALSE;
  120. currentIsBodyProcedure := FALSE;
  121. currentIsExclusive := FALSE;
  122. withEntries := NIL;
  123. SELF.cellsAreObjects := system.cellsAreObjects;
  124. COPY(backend, backendName);
  125. inConversion := 0;
  126. END InitChecker;
  127. (** report error **)
  128. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  129. VAR errModule: SyntaxTree.Module;
  130. BEGIN
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. Basic.ErrorC(diagnostics, errModule.sourceName, position, Basic.InvalidCode, message);
  134. error := TRUE;
  135. END Error;
  136. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  137. VAR errModule: SyntaxTree.Module;
  138. BEGIN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  141. END Warning;
  142. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  143. VAR errorMessage: ARRAY 256 OF CHAR;
  144. BEGIN
  145. Basic.Concat(errorMessage,msg," ", msg2);
  146. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  147. error := TRUE;
  148. END ErrorSS;
  149. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  150. VAR msg, msg2: ARRAY 256 OF CHAR;
  151. BEGIN
  152. COPY(msg1, msg);
  153. Strings.Append(msg, " = ");
  154. Basic.GetString(s, msg2);
  155. Strings.Append(msg, msg2);
  156. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  157. END InfoSS;
  158. (*** symbol lookup ***)
  159. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  160. **)
  161. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  162. VAR
  163. scope,baseScope: SyntaxTree.Scope;
  164. symbol, s: SyntaxTree.Symbol;
  165. ownerRecord,base: SyntaxTree.RecordType;
  166. BEGIN
  167. scope := inScope;
  168. symbol := NIL;
  169. WHILE (scope # NIL) & (symbol = NIL) DO
  170. symbol := scope.FindSymbol(name);
  171. s := NIL;
  172. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  173. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  175. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  176. END;
  177. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  178. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  179. ELSE
  180. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  181. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  182. symbol.MarkUsed;
  183. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  184. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  185. base := RecordBase(ownerRecord);
  186. IF (base # NIL) THEN
  187. baseScope := base.recordScope;
  188. symbol := Find(baseScope,name,FALSE);
  189. ELSE
  190. symbol := NIL;
  191. END;
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. END;
  196. END;
  197. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  198. END;
  199. IF (symbol # NIL) THEN
  200. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  201. ASSERT(phase = DeclarationPhase);
  202. ResolveSymbol(symbol)
  203. END;
  204. symbol.MarkUsed;
  205. END;
  206. RETURN symbol
  207. END Find;
  208. (*** types ***)
  209. (** find type declaration with name qualifiedIdentifier and return resolved type
  210. - check qualified identifier prefix, set scope to module scope if appropriate
  211. - check suffix in scope
  212. **)
  213. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  214. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  215. BEGIN
  216. result := NIL;
  217. prevScope := currentScope;
  218. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  219. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  220. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  221. IF symbol(SyntaxTree.Import).module = NIL THEN
  222. Error(qualifiedIdentifier.position,"module not loaded");
  223. result := SyntaxTree.invalidType;
  224. symbol := NIL;
  225. ELSE
  226. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  227. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  228. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  229. IF VerboseErrorMessage THEN
  230. Printout.Info("scope", currentScope);
  231. Printout.Info("symbol", symbol);
  232. END;
  233. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  234. END;
  235. END;
  236. ELSE
  237. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  238. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  239. symbol := NIL;
  240. END;
  241. ELSE
  242. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  243. IF symbol = NIL THEN
  244. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  245. IF VerboseErrorMessage THEN
  246. Printout.Info("Qualident",qualifiedIdentifier);
  247. Printout.Info("in scope",currentScope) ;
  248. END;
  249. END;
  250. END;
  251. IF symbol = NIL THEN (* error already handled *)
  252. typeDeclaration := NIL;
  253. result := SyntaxTree.invalidType;
  254. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  255. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  256. typeDeclaration := NIL;
  257. result := SyntaxTree.invalidType;
  258. ELSE
  259. currentScope := symbol.scope;
  260. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  261. result := ResolveType(typeDeclaration.declaredType);
  262. symbol.MarkUsed;
  263. ASSERT(result # NIL);
  264. END;
  265. currentScope := prevScope;
  266. RETURN result
  267. END ResolveNamedType;
  268. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  269. If node is currently being resolved then emit a cyclic definition error.
  270. Return TRUE only if node is fully resolved.
  271. **)
  272. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  273. VAR result: BOOLEAN;
  274. BEGIN
  275. IF SyntaxTree.Resolved IN x.state THEN
  276. result := FALSE
  277. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  278. Error(x.position,"cyclic definition");
  279. result := FALSE;
  280. ELSE
  281. result := TRUE;
  282. x.SetState(SyntaxTree.BeingResolved)
  283. END;
  284. RETURN result
  285. END TypeNeedsResolution;
  286. (** Return invalid type if x is currently being resolved, return x otherwise**)
  287. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  288. BEGIN
  289. IF SyntaxTree.Resolved IN x.state THEN
  290. RETURN x
  291. ELSE
  292. RETURN SyntaxTree.invalidType
  293. END;
  294. END ResolvedType;
  295. PROCEDURE VisitType*(x: SyntaxTree.Type);
  296. BEGIN
  297. ASSERT(x = SyntaxTree.invalidType);
  298. END VisitType;
  299. (** resolve basic type **)
  300. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  301. BEGIN
  302. IF TypeNeedsResolution(x) THEN
  303. x.SetState(SyntaxTree.Resolved);
  304. END;
  305. resolvedType := ResolvedType(x)
  306. END VisitBasicType;
  307. PROCEDURE VisitByteType*(x: SyntaxTree.ByteType);
  308. BEGIN
  309. VisitBasicType(x);
  310. END VisitByteType;
  311. (** resolve character type **)
  312. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  313. BEGIN
  314. VisitBasicType(x);
  315. END VisitCharacterType;
  316. PROCEDURE VisitBooleanType*(x: SyntaxTree.BooleanType);
  317. BEGIN
  318. VisitBasicType(x);
  319. END VisitBooleanType;
  320. PROCEDURE VisitSetType*(x: SyntaxTree.SetType);
  321. BEGIN
  322. VisitBasicType(x);
  323. END VisitSetType;
  324. PROCEDURE VisitAddressType*(x: SyntaxTree.AddressType);
  325. BEGIN
  326. VisitBasicType(x);
  327. END VisitAddressType;
  328. PROCEDURE VisitSizeType*(x: SyntaxTree.SizeType);
  329. BEGIN
  330. VisitBasicType(x);
  331. END VisitSizeType;
  332. PROCEDURE VisitAnyType*(x: SyntaxTree.AnyType);
  333. BEGIN
  334. VisitBasicType(x);
  335. END VisitAnyType;
  336. PROCEDURE VisitObjectType*(x: SyntaxTree.ObjectType);
  337. BEGIN
  338. VisitBasicType(x);
  339. END VisitObjectType;
  340. PROCEDURE VisitNilType*(x: SyntaxTree.NilType);
  341. BEGIN
  342. VisitBasicType(x);
  343. END VisitNilType;
  344. (** resolve integer type **)
  345. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  346. BEGIN
  347. VisitBasicType(x);
  348. END VisitIntegerType;
  349. (** resolve real type **)
  350. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  351. BEGIN
  352. VisitBasicType(x);
  353. END VisitFloatType;
  354. (** resolve complex type **)
  355. PROCEDURE VisitComplexType*(x: SyntaxTree.ComplexType);
  356. BEGIN
  357. VisitBasicType(x);
  358. END VisitComplexType;
  359. (**
  360. resolve string type: nothing to be done
  361. **)
  362. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  363. BEGIN
  364. IF TypeNeedsResolution(x) THEN
  365. x.SetState(SyntaxTree.Resolved);
  366. END;
  367. resolvedType := ResolvedType(x)
  368. END VisitStringType;
  369. (**
  370. check enumeration scope: enter symbols and check for duplicate names
  371. **)
  372. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: Basic.Integer);
  373. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: Basic.Integer; prevScope: SyntaxTree.Scope;
  374. BEGIN
  375. prevScope := currentScope;
  376. currentScope := x;
  377. e := x.firstConstant;
  378. nextHighest := highest;
  379. WHILE (e # NIL) DO
  380. Register(e,x,FALSE);
  381. IF SymbolNeedsResolution(e) THEN
  382. IF e.value # NIL THEN
  383. value := ConstantExpression(e.value);
  384. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  385. ELSE
  386. value := SyntaxTree.NewEnumerationValue(e.position,nextHighest+1);
  387. value.SetType(x.ownerEnumeration);
  388. END;
  389. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  390. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  391. IF nextHighest > highest THEN highest := nextHighest END;
  392. END;
  393. e.SetValue(value);
  394. CheckSymbolVisibility(e);
  395. e.SetType(x.ownerEnumeration);
  396. e.SetState(SyntaxTree.Resolved);
  397. END;
  398. e := e.nextConstant;
  399. END;
  400. currentScope := prevScope;
  401. END CheckEnumerationScope;
  402. (**
  403. resolve enumeration type: check enumeration scope
  404. **)
  405. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  406. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  407. lowest, highest: Basic.Integer;
  408. BEGIN
  409. IF TypeNeedsResolution(x) THEN
  410. IF x.enumerationBase # NIL THEN
  411. position := x.enumerationBase.position;
  412. baseType := ResolveType(x.enumerationBase);
  413. resolved := baseType.resolved;
  414. baseScope := NIL;
  415. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  416. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  417. Error(position, "base type is no enumeration type");
  418. ELSE
  419. enumerationBase := resolved(SyntaxTree.EnumerationType);
  420. lowest := enumerationBase.rangeHighest+1;
  421. END;
  422. x.SetEnumerationBase(baseType);
  423. ELSE lowest := 0;
  424. END;
  425. highest := lowest-1;
  426. CheckEnumerationScope(x.enumerationScope, highest);
  427. x.SetRange(lowest, highest);
  428. x.SetState(SyntaxTree.Resolved);
  429. END;
  430. resolvedType := ResolvedType(x);
  431. END VisitEnumerationType;
  432. (**
  433. resolve range type: nothing to be done
  434. **)
  435. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  436. BEGIN
  437. IF TypeNeedsResolution(x) THEN
  438. x.SetState(SyntaxTree.Resolved);
  439. END;
  440. resolvedType := ResolvedType(x)
  441. END VisitRangeType;
  442. (**
  443. resolve qualified type
  444. - find and resolve named type and set resolved type
  445. **)
  446. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  447. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  448. BEGIN
  449. IF TypeNeedsResolution(x) THEN
  450. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  451. x.SetResolved(type.resolved);
  452. x.SetState(SyntaxTree.Resolved);
  453. x.SetTypeDeclaration (typeDeclaration);
  454. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  455. x.SetResolved(SyntaxTree.invalidType);
  456. END;
  457. resolvedType := x;
  458. END VisitQualifiedType;
  459. (**
  460. resolve array type
  461. - check base type
  462. - array of math array forbidden
  463. - static array of open array forbidden
  464. **)
  465. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  466. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  467. BEGIN
  468. IF TypeNeedsResolution(x) THEN
  469. x.SetArrayBase(ResolveType(x.arrayBase));
  470. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  471. arrayBase := x.arrayBase.resolved;
  472. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  473. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  474. pointerType.SetPointerBase(arrayBase);
  475. pointerType.SetHidden(TRUE);
  476. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  477. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  478. ELSE
  479. x.SetArrayBase(pointerType);
  480. END;
  481. END;
  482. IF x.length # NIL THEN
  483. variableAccessed := FALSE;
  484. e := ResolveExpression(x.length);
  485. IF (e.resolved = NIL) THEN
  486. IF variableAccessed THEN
  487. Error(e.position, "forbidden variable access");
  488. END;
  489. IF CheckSizeType(e) THEN
  490. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  491. END;
  492. ELSE
  493. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  494. END;
  495. END;
  496. IF arrayBase IS SyntaxTree.ArrayType THEN
  497. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  498. Error(x.position,"forbidden static array of dynamic array");
  499. END;
  500. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  501. Error(x.position,"forbidden array mixed form");
  502. END;
  503. x.SetHasPointers(arrayBase.hasPointers);
  504. x.SetState(SyntaxTree.Resolved);
  505. END;
  506. resolvedType := ResolvedType(x);
  507. END VisitArrayType;
  508. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  509. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  510. BEGIN
  511. module := currentScope.ownerModule;
  512. IF module.name=name THEN
  513. (* do nothing *)
  514. ELSE
  515. moduleScope := module.moduleScope;
  516. import := moduleScope.FindImport(name);
  517. IF import = NIL THEN
  518. import := SyntaxTree.NewImport(position,name,name,TRUE);
  519. moduleScope.AddImport(import);
  520. Register(import,moduleScope,FALSE);
  521. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  522. VisitImport(import);
  523. ELSIF import.direct=FALSE THEN
  524. import.SetScope(module.moduleScope);
  525. import.SetDirect(TRUE);
  526. IF moduleScope.FindSymbol(import.name) = NIL THEN
  527. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  528. duplicate.SetContext(import.context);
  529. duplicate.SetModule(import.module);
  530. Register(duplicate,moduleScope,TRUE);
  531. VisitImport(duplicate);
  532. END;
  533. END;
  534. import.MarkUsed
  535. END;
  536. END ImportModule;
  537. (**
  538. resolve math array type
  539. - check base type
  540. - open math array of array forbidden
  541. - math array of tensor forbidden
  542. - static array of open array forbidden
  543. **)
  544. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  545. VAR arrayBase: SyntaxTree.Type; modifiers: SyntaxTree.Modifier; position: SyntaxTree.Position;
  546. BEGIN
  547. IF TypeNeedsResolution(x) THEN
  548. x.SetArrayBase(ResolveType(x.arrayBase));
  549. IF x.length # NIL THEN
  550. x.SetLength(ConstantIntegerGeq0(x.length));
  551. END;
  552. arrayBase := x.arrayBase;
  553. IF arrayBase # NIL THEN
  554. arrayBase := arrayBase.resolved;
  555. IF arrayBase = SyntaxTree.invalidType THEN
  556. (* error already handled *)
  557. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  558. Error(x.position,"forbidden array mixed form");
  559. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  560. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  561. Error(x.position,"forbidden Tensor Array mix")
  562. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  563. Error(x.position,"forbidden static array of dynamic array")
  564. END;
  565. END;
  566. IF x.form = SyntaxTree.Static THEN
  567. x.SetIncrement(system.SizeOf(arrayBase));
  568. END;
  569. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  570. END;
  571. modifiers := x.modifiers;
  572. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  573. CheckModifiers(modifiers, TRUE);
  574. x.SetState(SyntaxTree.Resolved);
  575. END;
  576. resolvedType := ResolvedType(x);
  577. END VisitMathArrayType;
  578. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  579. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  580. (1) Rec = RECORD ... END; Ptr <---> Rec
  581. Ptr = POINTER TO Rec; ^ |
  582. | |
  583. TypeDesc TypeDesc
  584. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  585. ^ /
  586. | /
  587. TypeDesc <-- /
  588. *)
  589. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  590. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  591. BEGIN
  592. Strings.IntToStr(x.position.start,number);
  593. COPY(prefix,name);
  594. Strings.Append(name,"@");
  595. Strings.Append(name,number);
  596. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  597. typeDeclaration.SetDeclaredType(x);
  598. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  599. x.SetTypeDeclaration(typeDeclaration);
  600. currentScope.AddTypeDeclaration(typeDeclaration);
  601. typeDeclaration.SetScope(currentScope);
  602. END AnonymousTypeDeclaration;
  603. (**
  604. deferred pointer type resolving
  605. - resolve base type
  606. - check that base type is a record or array type
  607. - if error then set base type to invalid type
  608. **)
  609. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  610. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  611. BEGIN
  612. ASSERT(type.pointerBase # NIL);
  613. position := type.pointerBase.position;
  614. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  615. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  616. (* not for pointers, a type is needed for the records only
  617. IF type.typeDeclaration = NIL THEN
  618. AnonymousTypeDeclaration(type);
  619. END;
  620. *)
  621. END;
  622. resolved := ResolveType(type.pointerBase);
  623. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  624. type.SetPointerBase(resolved);
  625. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  626. recordType := resolved.resolved(SyntaxTree.RecordType);
  627. IF recordType.isObject & (recordType.baseType # NIL) THEN
  628. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  629. Error(position, "base type of object must be a realtime object");
  630. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  631. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  632. END;
  633. END;
  634. END;
  635. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  636. Error(position, "realtime object contains references to non-realtime objects");
  637. END;
  638. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  639. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  640. Error(position, "forbidden unsafe at static array");
  641. ELS
  642. *)
  643. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  644. Error(position, "forbidden unsafe at multidimensional array");
  645. END;
  646. END;
  647. ELSE
  648. Error(position, "forbidden pointer base type");
  649. type.SetPointerBase(SyntaxTree.invalidType)
  650. END
  651. END FixPointerType;
  652. (**
  653. resolve pointer type
  654. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  655. **)
  656. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  657. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  658. modifiers: SyntaxTree.Modifier; position: Position;
  659. BEGIN
  660. IF TypeNeedsResolution(x) THEN
  661. modifiers := x.modifiers;
  662. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  663. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  664. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  665. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  666. x.SetUntraced(HasFlag(modifiers,Global.NameUntraced,position));
  667. (* inheritance cycle check
  668. example:
  669. A=POINTER TO RECORD(B) END;
  670. B=POINTER TO RECORD(A) END;
  671. *)
  672. IF x.pointerBase IS SyntaxTree.RecordType THEN
  673. recordType := x.pointerBase(SyntaxTree.RecordType);
  674. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  675. recordBaseType := ResolveType(recordType.baseType);
  676. recordType.SetBaseType(recordBaseType);
  677. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  678. recordType.SetAbstract(HasFlag(modifiers, Global.NameAbstract, position));
  679. END;
  680. CheckModifiers(modifiers, TRUE);
  681. typeFixes.Add(x,currentScope);
  682. x.SetState(SyntaxTree.Resolved);
  683. END;
  684. resolvedType := ResolvedType(x)
  685. END VisitPointerType;
  686. (**
  687. resolve port type
  688. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  689. **)
  690. PROCEDURE VisitPortType*(x: SyntaxTree.PortType);
  691. VAR value: Basic.Integer;
  692. BEGIN
  693. IF TypeNeedsResolution(x) THEN
  694. x.SetCellsAreObjects(cellsAreObjects);
  695. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  696. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  697. x.SetSize(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  698. ELSE
  699. x.SetSize(system.SizeOf(system.longintType));
  700. END;
  701. x.SetState(SyntaxTree.Resolved);
  702. END;
  703. resolvedType := ResolvedType(x)
  704. END VisitPortType;
  705. (**
  706. deferred procedure type resolving
  707. - resolve return type
  708. - traverse and resolve parameters
  709. **)
  710. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  711. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  712. BEGIN
  713. resolved := ResolveType(procedureType.returnType);
  714. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  715. Error(procedureType.position,"forbidden open array return type");
  716. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  717. Error(procedureType.position,"procedure with return type does not return");
  718. END;
  719. procedureType.SetReturnType(resolved);
  720. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  721. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  722. parameter.SetType(procedureType.returnType);
  723. parameter.SetAccess(SyntaxTree.Hidden);
  724. parameter.SetUntraced(procedureType.hasUntracedReturn);
  725. VisitParameter(parameter);
  726. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  727. END;
  728. (* process parameters *)
  729. parameter :=procedureType.firstParameter;
  730. WHILE (parameter # NIL) DO
  731. VisitParameter(parameter);
  732. parameter := parameter.nextParameter;
  733. END;
  734. parameter := procedureType.selfParameter;
  735. IF parameter # NIL THEN
  736. VisitParameter(parameter)
  737. END;
  738. END FixProcedureType;
  739. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  740. VAR prev,this: SyntaxTree.Modifier;
  741. BEGIN
  742. this := modifiers;prev := NIL;
  743. WHILE (this # NIL) & (this.identifier # name) DO
  744. prev := this; this := this.nextModifier;
  745. END;
  746. IF this # NIL THEN
  747. IF this.expression # NIL THEN
  748. Error(this.position,"unexpected expression");
  749. END;
  750. this.Resolved;
  751. position := this.position;
  752. RETURN TRUE
  753. ELSE
  754. RETURN FALSE
  755. END;
  756. END HasFlag;
  757. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: Basic.Integer): BOOLEAN;
  758. VAR prev,this: SyntaxTree.Modifier;
  759. BEGIN
  760. this := modifiers;prev := NIL;
  761. WHILE (this # NIL) & (this.identifier # name) DO
  762. prev := this; this := this.nextModifier;
  763. END;
  764. IF this # NIL THEN
  765. IF this.expression = NIL THEN
  766. Error(this.position,"expected expression value");
  767. ELSE
  768. this.SetExpression(ConstantExpression(this.expression));
  769. IF CheckIntegerValue(this.expression,value) THEN END;
  770. END;
  771. this.Resolved;
  772. position := this.position;
  773. RETURN TRUE
  774. ELSE RETURN FALSE
  775. END;
  776. END HasValue;
  777. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  778. VAR prev,this: SyntaxTree.Modifier;
  779. BEGIN
  780. this := modifiers;prev := NIL;
  781. WHILE (this # NIL) & (this.identifier # name) DO
  782. prev := this; this := this.nextModifier;
  783. END;
  784. IF this # NIL THEN
  785. IF this.expression = NIL THEN
  786. Error(this.position,"expected expression value");
  787. ELSE
  788. this.SetExpression(ConstantExpression(this.expression));
  789. IF CheckStringValue(this.expression,value) THEN END;
  790. END;
  791. this.Resolved;
  792. position := this.position;
  793. RETURN TRUE
  794. ELSE RETURN FALSE
  795. END;
  796. END HasStringValue;
  797. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  798. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  799. BEGIN
  800. IF cellsAreObjects THEN RETURN FALSE END;
  801. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  802. IF svalue = "A2" THEN
  803. RETURN TRUE
  804. END;
  805. END;
  806. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  807. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  808. END;
  809. RETURN FALSE;
  810. (*
  811. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  812. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  813. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  814. IF svalue[0] = "~" THEN
  815. Strings.TrimLeft(svalue, "~");
  816. IF svalue = backendName THEN
  817. RETURN TRUE;
  818. END;
  819. ELSIF svalue # backendName THEN
  820. RETURN TRUE;
  821. END;
  822. END;
  823. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  824. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  825. END;
  826. RETURN FALSE;
  827. *)
  828. END SkipImplementation;
  829. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  830. VAR this: SyntaxTree.Modifier;
  831. BEGIN
  832. this := modifiers;
  833. WHILE this # NIL DO
  834. IF ~this.resolved THEN
  835. IF checkUse THEN
  836. Error(this.position,"unexpected modifier");
  837. ELSE
  838. this.SetExpression(ResolveExpression(this.expression));
  839. this.Resolved;
  840. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  841. END;
  842. END;
  843. this := this.nextModifier
  844. END;
  845. END CheckModifiers;
  846. (**
  847. resolve procedure type
  848. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  849. **)
  850. PROCEDURE VisitProcedureType*(procedureType: SyntaxTree.ProcedureType);
  851. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position;
  852. BEGIN
  853. IF TypeNeedsResolution(procedureType) THEN
  854. modifiers := procedureType.modifiers;
  855. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  856. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  857. procedureType.SetInterrupt(TRUE);
  858. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  859. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  860. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  861. ELSIF HasFlag(modifiers,Global.NamePlatformCC, position) THEN
  862. IF system.platformCallingConvention = SyntaxTree.UndefinedCallingConvention THEN
  863. Error(position, "undefined platform calling convention");
  864. ELSE
  865. procedureType.SetCallingConvention(system.platformCallingConvention);
  866. END;
  867. END;
  868. IF HasFlag(modifiers, Global.NameNoReturn,position) THEN procedureType.SetNoReturn(TRUE) END;
  869. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(LONGINT (value)) END; (* TODO: fix explicit integer truncation *)
  870. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  871. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  872. CheckModifiers(modifiers, TRUE);
  873. modifiers := procedureType.returnTypeModifiers;
  874. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  875. CheckModifiers(modifiers, TRUE);
  876. typeFixes.Add(procedureType,currentScope);
  877. procedureType.SetHasPointers(procedureType.isDelegate);
  878. procedureType.SetState(SyntaxTree.Resolved);
  879. END;
  880. resolvedType := ResolvedType(procedureType)
  881. END VisitProcedureType;
  882. (** check and resolve record type
  883. - check base type: must be record, math array or array-structured object type
  884. - check declarations
  885. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  886. **)
  887. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  888. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  889. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  890. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  891. hasPointers: BOOLEAN;
  892. modifiers: SyntaxTree.Modifier;
  893. value: Basic.Integer;
  894. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  895. BEGIN
  896. type := type.resolved;
  897. IF (type IS SyntaxTree.PointerType) &
  898. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  899. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  900. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  901. RETURN TRUE
  902. ELSE
  903. RETURN FALSE
  904. END;
  905. END IsPointerToRecord;
  906. BEGIN
  907. IF TypeNeedsResolution(x) THEN
  908. hasPointers := FALSE;
  909. modifiers := x.modifiers;
  910. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(LONGINT(value)*system.dataUnit) (* TODO: fix explicit integer truncation *)
  911. END;
  912. IF HasFlag(modifiers,Global.NameAbstract,position) THEN x.SetAbstract(TRUE) END;
  913. CheckModifiers(modifiers, TRUE);
  914. IF x.baseType # NIL THEN
  915. position := x.baseType.position;
  916. baseType := ResolveType(x.baseType);
  917. resolved := baseType.resolved;
  918. hasPointers := hasPointers OR resolved.hasPointers;
  919. IF x.isObject THEN (* object *)
  920. ASSERT(x.pointerType # NIL);
  921. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  922. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  923. baseType := NIL
  924. ELSIF IsPointerToRecord(resolved,recordType) THEN
  925. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  926. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  927. ELSE
  928. Error(position,"object does not extend pointer to record, object or math array ")
  929. END;
  930. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  931. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  932. ELSIF IsPointerToRecord(resolved,recordType) THEN
  933. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  934. ELSIF resolved IS SyntaxTree.RecordType THEN
  935. ELSE
  936. Error(position,"pointer to record does not extend pointer to record or record")
  937. END;
  938. ELSE
  939. IF resolved IS SyntaxTree.RecordType THEN
  940. ELSE
  941. Error(position,"record does not extend record")
  942. END;
  943. END;
  944. x.SetBaseType(baseType);
  945. IF x.Level() > 15 THEN
  946. Error(position, "record/object inheritance level too high");
  947. (* note:
  948. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  949. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  950. inheritance history of a type.
  951. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  952. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  953. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  954. *)
  955. END;
  956. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  957. Error(position, "invalid inheritance of disposable types");
  958. END;
  959. END;
  960. Declarations(x.recordScope, FALSE, {0});
  961. x.SetState(SyntaxTree.Resolved);
  962. Declarations(x.recordScope, FALSE, {1});
  963. ResolveArrayStructure(x);
  964. (* computation of sizes and offsets skipped -> done in backend / system *)
  965. recordBase := x.GetBaseRecord();
  966. IF recordBase = NIL THEN numberMethods := 0
  967. ELSE numberMethods := recordBase.recordScope.numberMethods
  968. END;
  969. isRealtime := TRUE;
  970. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  971. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  972. END;
  973. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  974. WHILE symbol # NIL DO
  975. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  976. IF symbol IS SyntaxTree.Variable THEN
  977. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  978. END;
  979. IF symbol IS SyntaxTree.Procedure THEN
  980. procedure := symbol(SyntaxTree.Procedure);
  981. IF procedure.super # NIL THEN
  982. procedure.SetMethodNumber(procedure.super.methodNumber);
  983. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  984. procedure.SetMethodNumber(numberMethods);
  985. INC(numberMethods);
  986. END;
  987. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  988. Error(procedure.position,"realtime procedure in non-realtime object")
  989. END;
  990. END;
  991. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  992. Error(symbol.position,"non-realtime symbol in realtime object")
  993. END;
  994. symbol := symbol.nextSymbol;
  995. END;
  996. IF isRealtime THEN x.SetRealtime(TRUE) END;
  997. x.recordScope.SetNumberMethods(numberMethods);
  998. (* TODO: is this needed anymore? *)
  999. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  1000. Error(x.position,"object extends a record")
  1001. END;
  1002. IF ~x.isAbstract THEN
  1003. IF x.recordScope.AbstractProcedure(x.recordScope) # NIL THEN
  1004. Error(x.position, "non-abstract object contains abstract procedure");
  1005. END;
  1006. ELSE
  1007. IF x.recordScope.AbstractProcedure(x.recordScope) = NIL THEN
  1008. Error(x.position, "abstract object does not contain an abstract method");
  1009. END;
  1010. END;
  1011. IF (x.typeDeclaration = NIL) THEN
  1012. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  1013. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  1014. (*
  1015. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  1016. AnonymousTypeDeclaration(x,name);
  1017. *)
  1018. ELSE
  1019. AnonymousTypeDeclaration(x,"Anonymous");
  1020. END;
  1021. END;
  1022. x.SetHasPointers(hasPointers);
  1023. x.SetState(SyntaxTree.Resolved);
  1024. END;
  1025. resolvedType := ResolvedType(x);
  1026. END VisitRecordType;
  1027. (** check and resolve cell type
  1028. - check base type: must be cell
  1029. - check declarations
  1030. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1031. **)
  1032. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  1033. VAR
  1034. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1035. modifier: SyntaxTree.Modifier; position: Position; value: Basic.Integer; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1036. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1037. recordBase: SyntaxTree.RecordType;
  1038. numberMethods: LONGINT;
  1039. int: Basic.Integer;
  1040. real: LONGREAL;
  1041. bool: BOOLEAN;
  1042. set: Basic.Set;
  1043. v: SyntaxTree.Expression;
  1044. str: Scanner.StringType;
  1045. atype: SyntaxTree.ArrayType;
  1046. prev: SyntaxTree.Scope;
  1047. BEGIN
  1048. IF TypeNeedsResolution(x) THEN
  1049. recordBase := NIL;
  1050. IF cellsAreObjects THEN
  1051. IF x.baseType = NIL THEN
  1052. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1053. ImportModule(qualifiedIdentifier.prefix, x.position);
  1054. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1055. x.SetBaseType(ResolveType(x.baseType));
  1056. recordBase := x.GetBaseRecord();
  1057. IF recordBase = NIL THEN
  1058. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1059. END;
  1060. ELSE
  1061. x.SetBaseType(ResolveType(x.baseType));
  1062. END;
  1063. ELSE
  1064. x.SetBaseType(ResolveType(x.baseType));
  1065. END;
  1066. IF recordBase = NIL THEN numberMethods := 0
  1067. ELSE numberMethods := recordBase.recordScope.numberMethods
  1068. END;
  1069. modifier := x.modifiers;
  1070. (*IF ~x.isCellNet THEN*)
  1071. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1072. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1073. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1074. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1075. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1076. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1077. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1078. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1079. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1080. symbol := system.activeCellsCapabilities;
  1081. WHILE symbol # NIL DO
  1082. IF HasFlag(modifier, symbol.name, position) THEN END;
  1083. symbol := symbol.nextSymbol;
  1084. END;
  1085. modifier := x.modifiers;
  1086. WHILE (modifier # NIL) DO
  1087. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1088. IF modifier.expression # NIL THEN
  1089. v := ConstantExpression(modifier.expression);
  1090. property.SetValue(v);
  1091. IF IsIntegerValue(modifier.expression, int) THEN
  1092. (*property.SetValue(modifier.expression);*)
  1093. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1094. property.SetType(system.longintType);
  1095. ELSIF IsRealValue(modifier.expression, real) THEN
  1096. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1097. property.SetType(system.longrealType);
  1098. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1099. property.SetType(system.booleanType);
  1100. ELSIF IsSetValue(modifier.expression, set) THEN
  1101. property.SetType(system.setType);
  1102. ELSIF IsStringValue(modifier.expression, str) THEN
  1103. (*property.SetValue(modifier.expression);*)
  1104. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1105. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1106. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1107. property.SetType(atype);
  1108. ELSE
  1109. Error(modifier.position, "unsupported property type");
  1110. END;
  1111. ELSE (* flag property *)
  1112. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1113. property.SetType(system.booleanType);
  1114. END;
  1115. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1116. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1117. x.AddProperty(property);
  1118. modifier := modifier.nextModifier;
  1119. END;
  1120. CheckModifiers(modifier, FALSE);
  1121. Declarations(x.cellScope, SkipImplementation(x),{0,1});
  1122. (* process parameters *)
  1123. prev := currentScope;
  1124. currentScope := x.cellScope;
  1125. parameter :=x.firstParameter;
  1126. WHILE (parameter # NIL) DO
  1127. VisitParameter(parameter);
  1128. type := parameter.type.resolved;
  1129. IF ~(type IS SyntaxTree.PortType) THEN
  1130. WHILE IsStaticArray(type, type, len) DO
  1131. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1132. END;
  1133. WHILE IsDynamicArray(type, type) DO
  1134. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1135. END;
  1136. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1137. Error(parameter.position, "invalid type, must be port or static array of port ");
  1138. END;
  1139. END;
  1140. parameter := parameter.nextParameter;
  1141. END;
  1142. currentScope := prev;
  1143. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1144. WHILE symbol # NIL DO
  1145. IF symbol IS SyntaxTree.Variable THEN
  1146. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1147. END;
  1148. symbol := symbol.nextSymbol;
  1149. END;
  1150. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1151. IF (x.typeDeclaration = NIL) THEN
  1152. AnonymousTypeDeclaration(x,"Anonymous");
  1153. END;
  1154. x.SetState(SyntaxTree.Resolved);
  1155. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1156. Warning(x.position, "Forbidden empty Body.");
  1157. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1158. Warning(x.position, "Non-empty body for an engine?");
  1159. END;
  1160. END;
  1161. resolvedType := ResolvedType(x);
  1162. END VisitCellType;
  1163. (* check if an object is an array-structured object type
  1164. - determine the array structure
  1165. - collect operators from top to bottom in the inheritance hierarchy
  1166. - check if LEN operator is declared
  1167. - determine number of possible index operators
  1168. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1169. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1170. *)
  1171. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1172. VAR
  1173. indexOperatorCount, i: LONGINT;
  1174. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1175. isTensor: BOOLEAN;
  1176. BEGIN
  1177. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1178. (* determine array structure *)
  1179. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1180. END;
  1181. IF recordType.HasArrayStructure() THEN
  1182. (* the object is an ASOT *)
  1183. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1184. (* reset array access operators *)
  1185. arrayAccessOperators.len := NIL;
  1186. arrayAccessOperators.generalRead := NIL;
  1187. arrayAccessOperators.generalWrite := NIL;
  1188. IF isTensor THEN
  1189. (* all operators of dimensionalities 1 to max *)
  1190. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1191. ELSE
  1192. (* all operators of certain dimensionality *)
  1193. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1194. END;
  1195. NEW(arrayAccessOperators.read, indexOperatorCount);
  1196. NEW(arrayAccessOperators.write, indexOperatorCount);
  1197. FOR i := 0 TO indexOperatorCount - 1 DO
  1198. arrayAccessOperators.read[i] := NIL;
  1199. arrayAccessOperators.write[i] := NIL
  1200. END;
  1201. (* collect access operators in the record scope *)
  1202. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1203. IF arrayAccessOperators.len = NIL THEN
  1204. (* TODO: think about making this operator optional for static array structures *)
  1205. Error(recordType.position, "LEN operator missing")
  1206. END;
  1207. (* show error messages *)
  1208. IF isTensor THEN
  1209. (* require ARRAY [*] OF RANGE *)
  1210. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1211. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1212. ELSE
  1213. (* forbid ARRAY [*] OF RANGE *)
  1214. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1215. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1216. (* require RANGE, RANGE, ... RANGE *)
  1217. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1218. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1219. END;
  1220. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1221. ELSE
  1222. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1223. IF recordType.recordScope.firstOperator # NIL THEN
  1224. RETURN;
  1225. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1226. END
  1227. END
  1228. END ResolveArrayStructure;
  1229. (** collect array access operators in a record scope **)
  1230. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1231. VAR
  1232. baseType: SyntaxTree.Type;
  1233. operator: SyntaxTree.Operator;
  1234. isReadOperator, isGeneralOperator: BOOLEAN;
  1235. indexListSize, indexListKind, hashValue: LONGINT;
  1236. BEGIN
  1237. (* if a parent record scope exists, collect the operators there first *)
  1238. baseType := recordScope.ownerRecord.baseType;
  1239. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1240. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1241. END;
  1242. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1243. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1244. END;
  1245. (* go through all operators in the current record scope *)
  1246. operator := recordScope.firstOperator;
  1247. WHILE operator # NIL DO
  1248. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1249. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1250. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1251. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1252. IF isGeneralOperator THEN
  1253. IF isReadOperator THEN
  1254. arrayAccessOperators.generalRead := operator
  1255. ELSE
  1256. arrayAccessOperators.generalWrite := operator
  1257. END
  1258. ELSE
  1259. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1260. IF isReadOperator THEN
  1261. arrayAccessOperators.read[hashValue] := operator
  1262. ELSE
  1263. arrayAccessOperators.write[hashValue] := operator
  1264. END
  1265. END
  1266. END
  1267. ELSE
  1268. Error(operator.position, 'invalid operator')
  1269. END;
  1270. operator := operator.nextOperator
  1271. END
  1272. END CollectArrayAccessOperators;
  1273. (** the hash value of an index operator **)
  1274. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1275. VAR result: LONGINT;
  1276. BEGIN
  1277. IF isTensor THEN
  1278. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1279. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1280. ELSE
  1281. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1282. END
  1283. ELSE
  1284. result := indexListKind
  1285. END;
  1286. RETURN result
  1287. END IndexOperatorHash;
  1288. (** 2 to the power of exponent **)
  1289. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1290. VAR result, i: LONGINT;
  1291. BEGIN
  1292. result := 1;
  1293. FOR i := 1 TO exponent DO
  1294. result := result * 2;
  1295. END;
  1296. RETURN result
  1297. END TwoToThePowerOf;
  1298. (** check if a LEN operator has a correct signature. i.e.
  1299. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF <LENTYPE>;'
  1300. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF <LENTYPE>;'
  1301. **)
  1302. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1303. VAR
  1304. procedureType: SyntaxTree.ProcedureType;
  1305. returnedArrayType: SyntaxTree.MathArrayType;
  1306. result: BOOLEAN;
  1307. BEGIN
  1308. result := FALSE;
  1309. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1310. IF (procedureType.numberParameters = 0) THEN
  1311. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1312. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1313. IF system.lenType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1314. IF returnedArrayType.form = SyntaxTree.Open THEN
  1315. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1316. result := TRUE
  1317. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1318. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1319. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1320. result := TRUE
  1321. END
  1322. END
  1323. END
  1324. END
  1325. END;
  1326. IF result THEN
  1327. (* export symbol automatically *)
  1328. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1329. ELSE
  1330. Error(operator.position, "LEN operator with invalid signature");
  1331. END;
  1332. RETURN result
  1333. END CheckLenOperator;
  1334. (** check if an index operator has a correct signature. i.e.
  1335. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1336. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1337. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1338. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1339. - determine if it is a read or write operator (existance of return type)
  1340. - check index parameters
  1341. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1342. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1343. [LONGINT] -> binary 0 -> 0
  1344. [RANGE] -> binary 1 -> 1
  1345. [LONGINT, LONGINT] -> binary 00 -> 0
  1346. [LONGINT, RANGE] -> binary 01 -> 1
  1347. [RANGE, LONGINT] -> binary 10 -> 2
  1348. [RANGE, RANGE] -> binary 11 -> 3
  1349. etc.
  1350. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1351. - for read operators, check if return type matches the type of data that is read
  1352. - for write operators, check if last parameter type matches the type of data that is written
  1353. **)
  1354. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1355. VAR
  1356. elementType, otherElementType, dataType: SyntaxTree.Type;
  1357. procedureType: SyntaxTree.ProcedureType;
  1358. mathArrayType: SyntaxTree.MathArrayType;
  1359. parameter: SyntaxTree.Parameter;
  1360. parameterCount, rangeCount, i: LONGINT;
  1361. hasTypeError: BOOLEAN;
  1362. BEGIN
  1363. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1364. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1365. (* determine if it is a read or write operator *)
  1366. isReadOperator := (procedureType.returnType # NIL);
  1367. IF isReadOperator THEN
  1368. indexListSize := parameterCount;
  1369. ELSE
  1370. indexListSize := parameterCount - 1;
  1371. END;
  1372. IF indexListSize < 1 THEN
  1373. Error(operator.position, "index operator with too few parameters");
  1374. RETURN FALSE
  1375. END;
  1376. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1377. (* general operator *)
  1378. isGeneralOperator := TRUE;
  1379. IF indexListSize > 1 THEN
  1380. Error(operator.position, "index operator with too many parameters");
  1381. RETURN FALSE
  1382. END;
  1383. (* ARRAY [*] OF RANGE*)
  1384. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1385. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1386. Error(operator.position, "index parameter not dynamic math array of range");
  1387. RETURN FALSE
  1388. END;
  1389. parameter := procedureType.firstParameter.nextParameter
  1390. ELSE
  1391. (* fixed-dim. operator *)
  1392. isGeneralOperator := FALSE;
  1393. (* check number of index parameters *)
  1394. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1395. (* for tensors, limited to a certain size *)
  1396. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1397. Error(operator.position, "too many index parameters for tensor");
  1398. RETURN FALSE
  1399. END
  1400. ELSE
  1401. (* for non-tensors, depends on dimensionality *)
  1402. IF indexListSize # arrayStructure.Dimensionality() THEN
  1403. Error(operator.position, "index parameter count does not match dimensionality");
  1404. RETURN FALSE
  1405. END
  1406. END;
  1407. (* go through all index parameters
  1408. - count the number of ranges
  1409. - determine the index list kind number
  1410. *)
  1411. indexListKind := 0;
  1412. rangeCount := 0;
  1413. parameter := procedureType.firstParameter;
  1414. FOR i := 1 TO indexListSize DO
  1415. indexListKind := indexListKind * 2;
  1416. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1417. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1418. INC(indexListKind);
  1419. INC(rangeCount)
  1420. ELSE
  1421. Error(parameter.position, "integer or range expected");
  1422. RETURN FALSE
  1423. END;
  1424. parameter := parameter.nextParameter
  1425. END;
  1426. END;
  1427. (*
  1428. - for read operators: check type of last parameter
  1429. - for write operators: check return type
  1430. *)
  1431. IF isReadOperator THEN
  1432. dataType := procedureType.returnType (* the return type *)
  1433. ELSE
  1434. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1435. END;
  1436. elementType := arrayStructure.ElementType();
  1437. hasTypeError := FALSE;
  1438. IF isGeneralOperator THEN
  1439. (* ARRAY [?] OF <Element> *)
  1440. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1441. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1442. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1443. hasTypeError := TRUE
  1444. END
  1445. ELSE
  1446. hasTypeError := TRUE
  1447. END
  1448. ELSE
  1449. IF rangeCount = 0 THEN
  1450. (* <Element> *)
  1451. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1452. ELSE
  1453. (* ARRAY [*, *, ..., *] OF <Element> *)
  1454. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1455. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1456. IF mathArrayType.IsFullyDynamic() THEN
  1457. IF mathArrayType.Dimensionality() = rangeCount THEN
  1458. otherElementType := mathArrayType.ElementType();
  1459. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1460. ELSE
  1461. hasTypeError := TRUE
  1462. END
  1463. ELSE
  1464. hasTypeError := TRUE
  1465. END
  1466. ELSE
  1467. hasTypeError := TRUE
  1468. END
  1469. END
  1470. END;
  1471. IF hasTypeError THEN
  1472. IF isReadOperator THEN
  1473. Error(operator.position, "return type does not match")
  1474. ELSE
  1475. Error(parameter.position, "type of last parameter does not match")
  1476. END;
  1477. RETURN FALSE
  1478. END;
  1479. (* export symbol automatically *)
  1480. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1481. RETURN TRUE
  1482. END CheckIndexOperator;
  1483. (** resolve all pending types (late resolving).
  1484. - type fixes are resolved at the end of the declaration phase
  1485. - type fixes may imply new type fixes that are also entered at the end of the list
  1486. **)
  1487. PROCEDURE FixTypes;
  1488. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1489. BEGIN
  1490. prevScope := currentScope;
  1491. p := typeFixes.Get(currentScope);
  1492. WHILE p # NIL DO
  1493. ASSERT(currentScope # NIL);
  1494. ASSERT(p IS SyntaxTree.Type);
  1495. IF p IS SyntaxTree.PointerType THEN
  1496. FixPointerType(p(SyntaxTree.PointerType))
  1497. ELSIF p IS SyntaxTree.ProcedureType THEN
  1498. FixProcedureType(p(SyntaxTree.ProcedureType))
  1499. ELSE
  1500. HALT(100);
  1501. END;
  1502. p := typeFixes.Get(currentScope);
  1503. END;
  1504. currentScope :=prevScope;
  1505. END FixTypes;
  1506. (**
  1507. resolve type x
  1508. - if x is nil then return nil
  1509. - if x cannot be resolved then the result is invalidType else the result is x
  1510. - the resolved type is entered into x.resolved
  1511. **)
  1512. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1513. VAR prev,resolved: SyntaxTree.Type;
  1514. BEGIN
  1515. prev := resolvedType;
  1516. resolvedType := SyntaxTree.invalidType;
  1517. IF x = NIL THEN resolvedType := NIL
  1518. ELSE VType(x); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1519. END;
  1520. resolved := resolvedType;
  1521. resolvedType := prev;
  1522. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1523. RETURN resolved
  1524. END ResolveType;
  1525. (*** compatibility rules ***)
  1526. (**
  1527. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1528. **)
  1529. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1530. VAR result: SyntaxTree.Type;
  1531. BEGIN
  1532. result := SyntaxTree.invalidType;
  1533. IF type = NIL THEN Error(position, "expression of type NIL");
  1534. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1535. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1536. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1537. ELSE result := type.resolved
  1538. END;
  1539. RETURN result
  1540. END RegularType;
  1541. (** returns signature compatibility of procedure types this and to
  1542. - if not compatible then error is reported
  1543. - compatibility means type equality
  1544. **)
  1545. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1546. VAR result: BOOLEAN;
  1547. BEGIN
  1548. result := SameType(to,this);
  1549. IF ~result THEN
  1550. Error(position, "signature incompatible");
  1551. IF VerboseErrorMessage THEN
  1552. Printout.Info("this",this);
  1553. Printout.Info("to",to);
  1554. END;
  1555. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1556. Error(position, "signature incompatible: realtime flag must be inherited");
  1557. END;
  1558. RETURN result
  1559. END SignatureCompatible;
  1560. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1561. - for var parameters compatibility means same type except for
  1562. - formal is of open array of system byte
  1563. - formal is of record type
  1564. - formal is of open array type
  1565. - formal is of open math array type
  1566. - for value parameters compatibllity means assignment compatibility except for
  1567. - formal is of open array type
  1568. if compatible the return true else report error and return false
  1569. **)
  1570. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1571. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1572. BEGIN
  1573. formalType := RegularType(formal.position,formal.type);
  1574. actualType := RegularType(actual.position,actual.type);
  1575. error := FALSE;
  1576. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1577. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1578. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1579. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1580. END;
  1581. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1582. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1583. ELSIF ~IsVariable(actual) THEN
  1584. result := FALSE; error := TRUE;
  1585. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1586. Error(actual.position,"not a variable: no operator for writing");
  1587. ELSE
  1588. Error(actual.position,"is not a variable");
  1589. END;
  1590. IF VerboseErrorMessage THEN
  1591. Printout.Info("actual",actual);
  1592. Printout.Info("formal",formal);
  1593. END;
  1594. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1595. result := CompatibleTo(system,actualType,formalType);
  1596. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1597. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1598. ELSIF IsUnsafePointer(formalType) & IsUnsafePointer(actualType) THEN
  1599. result := TRUE;
  1600. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1601. IF IsArrayStructuredObjectType(actualType) THEN
  1602. actualType := MathArrayStructureOfType(actualType)
  1603. END;
  1604. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1605. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1606. Error(actual.position,"incompatible non-static actual type");
  1607. END;
  1608. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1609. Error(actual.position,"incompatible tensor (use a range expression)");
  1610. END;
  1611. ELSE
  1612. result := SameType(actualType,formalType)
  1613. END
  1614. ELSE
  1615. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1616. actualType := system.characterType;
  1617. END;
  1618. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1619. result := TRUE; (* special rule for WINAPI parameters *)
  1620. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1621. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1622. ELSE
  1623. result := CompatibleTo(system,actualType,formalType);
  1624. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1625. Error(actual.position,"incompatible non-static actual type");
  1626. END;
  1627. END;
  1628. END;
  1629. IF ~result & ~error THEN
  1630. Error(actual.position,"incompatible parameter");
  1631. IF VerboseErrorMessage THEN
  1632. Printout.Info("actual",actual);
  1633. Printout.Info("formal",formal);
  1634. END;
  1635. END;
  1636. RETURN result
  1637. END ParameterCompatible;
  1638. (** check compatibility for expressions of the form left := right
  1639. - if compatible then return true else error report and return false
  1640. - check if left is variable
  1641. - check compatiblity
  1642. **)
  1643. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1644. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1645. BEGIN
  1646. result := FALSE;
  1647. leftType := RegularType(left.position,left.type);
  1648. rightType := RegularType(right.position,right.type);
  1649. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1650. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1651. END;
  1652. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1653. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1654. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1655. (* error already handled *)
  1656. result := TRUE;
  1657. ELSIF ~IsVariable(left) THEN
  1658. Error(left.position,"is not a variable");
  1659. IF VerboseErrorMessage THEN
  1660. Printout.Info("left",left);
  1661. Printout.Info("right",right);
  1662. END;
  1663. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1664. result := TRUE;
  1665. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1666. result := TRUE
  1667. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1668. Error(left.position,"incompatible assignment");
  1669. IF VerboseErrorMessage THEN
  1670. Printout.Info("left",left);
  1671. Printout.Info("right",right);
  1672. END;
  1673. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1674. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1675. Error(right.position,"forbidden assignment of a nested procedure");
  1676. ELSE
  1677. result := TRUE
  1678. END;
  1679. RETURN result
  1680. END AssignmentCompatible;
  1681. (*** values ***)
  1682. (** check and resolve integer value **)
  1683. PROCEDURE VisitIntegerValue*(value: SyntaxTree.IntegerValue);
  1684. VAR integer: Basic.Integer;
  1685. BEGIN
  1686. integer := value(SyntaxTree.IntegerValue).value;
  1687. value.SetType(Global.GetIntegerType(system,integer));
  1688. resolvedExpression := value
  1689. END VisitIntegerValue;
  1690. (** check and resolve real value **)
  1691. PROCEDURE VisitRealValue*(value: SyntaxTree.RealValue);
  1692. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1693. BEGIN
  1694. subtype := value(SyntaxTree.RealValue).subtype;
  1695. IF subtype = Scanner.Real THEN
  1696. type := system.realType
  1697. ELSIF subtype = Scanner.Longreal THEN
  1698. type := system.longrealType
  1699. ELSE
  1700. HALT(100)
  1701. END;
  1702. value.SetType(type);
  1703. resolvedExpression := value
  1704. END VisitRealValue;
  1705. (** check and resolve complex value **)
  1706. PROCEDURE VisitComplexValue*(value: SyntaxTree.ComplexValue);
  1707. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1708. BEGIN
  1709. subtype := value(SyntaxTree.ComplexValue).subtype;
  1710. IF subtype = Scanner.Real THEN
  1711. type := system.complexType
  1712. ELSIF subtype = Scanner.Longreal THEN
  1713. type := system.longcomplexType
  1714. ELSE
  1715. HALT(100)
  1716. END;
  1717. value.SetType(type);
  1718. resolvedExpression := value
  1719. END VisitComplexValue;
  1720. (** check and resolve set value **)
  1721. PROCEDURE VisitSetValue*(value: SyntaxTree.SetValue);
  1722. BEGIN
  1723. value.SetType(system.setType);
  1724. resolvedExpression := value
  1725. END VisitSetValue;
  1726. (** check and resolve set value **)
  1727. PROCEDURE VisitMathArrayValue*(value: SyntaxTree.MathArrayValue);
  1728. BEGIN
  1729. value.SetType(SyntaxTree.invalidType);
  1730. resolvedExpression := value
  1731. END VisitMathArrayValue;
  1732. (** check and resolve boolean value **)
  1733. PROCEDURE VisitBooleanValue*(value: SyntaxTree.BooleanValue);
  1734. BEGIN
  1735. value.SetType(system.booleanType);
  1736. resolvedExpression := value
  1737. END VisitBooleanValue;
  1738. (** check and resolve string value **)
  1739. PROCEDURE VisitStringValue*(value: SyntaxTree.StringValue);
  1740. BEGIN
  1741. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1742. resolvedExpression := value
  1743. END VisitStringValue;
  1744. (** check and resolve character value **)
  1745. PROCEDURE VisitCharacterValue*(value: SyntaxTree.CharacterValue);
  1746. BEGIN
  1747. value.SetType(system.characterType);
  1748. resolvedExpression := value
  1749. END VisitCharacterValue;
  1750. (** check and resolve nil value **)
  1751. PROCEDURE VisitNilValue*(value: SyntaxTree.NilValue);
  1752. BEGIN
  1753. value.SetType(system.nilType);
  1754. resolvedExpression := value
  1755. END VisitNilValue;
  1756. (** check and resolve enumerator value **)
  1757. PROCEDURE VisitEnumerationValue*(value: SyntaxTree.EnumerationValue);
  1758. BEGIN
  1759. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1760. ASSERT(value.type # NIL);
  1761. resolvedExpression := value
  1762. END VisitEnumerationValue;
  1763. (*** expressions ***)
  1764. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1765. - check all elements on integer type
  1766. - if element range is constant, then check lower and upper bound
  1767. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1768. if an error occurs then report error and return invalidExpression
  1769. **)
  1770. PROCEDURE VisitSet*(set: SyntaxTree.Set);
  1771. VAR
  1772. i: LONGINT;
  1773. element: SyntaxTree.Expression;
  1774. constant: BOOLEAN;
  1775. elements: SyntaxTree.ExpressionList;
  1776. s: Basic.Set;
  1777. result: SyntaxTree.Expression;
  1778. value: SyntaxTree.Value;
  1779. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1780. VAR
  1781. left, right: SyntaxTree.Expression;
  1782. elementResult: SyntaxTree.Expression;
  1783. leftInteger, rightInteger: Basic.Integer;
  1784. BEGIN
  1785. (* set context of range *)
  1786. IF element IS SyntaxTree.RangeExpression THEN
  1787. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1788. END;
  1789. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1790. IF elementResult = SyntaxTree.invalidExpression THEN
  1791. (* error already reported *)
  1792. constant := FALSE
  1793. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1794. (* the element is a range expression *)
  1795. (* extract left and right hand side of range *)
  1796. left := elementResult(SyntaxTree.RangeExpression).first;
  1797. right := elementResult(SyntaxTree.RangeExpression).last;
  1798. (* guaranteed by VisitRangeExpression: *)
  1799. ASSERT((left # NIL) & (right # NIL));
  1800. ASSERT(system.lenType.SameType(left.type.resolved) & system.lenType.SameType(right.type.resolved));
  1801. ELSE
  1802. (* the element is not a range expression *)
  1803. (* check type and add conversion if needed *)
  1804. IF IsIntegerType(elementResult.type.resolved) THEN
  1805. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1806. ELSE
  1807. Error(elementResult.position, "non integer element in set");
  1808. elementResult := SyntaxTree.invalidExpression;
  1809. constant := FALSE
  1810. END;
  1811. left := elementResult;
  1812. right := elementResult
  1813. END;
  1814. IF elementResult # SyntaxTree.invalidExpression THEN
  1815. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1816. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1817. Error(left.position,"not allowed set integer value");
  1818. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1819. Error(right.position,"not allowed set integer value");
  1820. END
  1821. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1822. Error(right.position,"not allowed set integer value");
  1823. ELSE
  1824. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1825. s := {};
  1826. ELSE
  1827. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1828. IF leftInteger < 0 THEN leftInteger := 0 END;
  1829. (*!!!!!!!!! this is a hack !!!!!!! *)
  1830. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1831. extends the range x..31 to x..63 !!!!!! *)
  1832. s := s + {leftInteger..rightInteger};
  1833. END;
  1834. END;
  1835. ELSE
  1836. constant := FALSE;
  1837. END
  1838. END;
  1839. RETURN elementResult
  1840. END CheckElement;
  1841. BEGIN
  1842. result := set; constant := TRUE; s := {}; elements := set.elements;
  1843. IF elements # NIL THEN
  1844. FOR i := 0 TO elements.Length()-1 DO
  1845. element := elements.GetExpression(i);
  1846. element := CheckElement(element);
  1847. IF element = SyntaxTree.invalidExpression THEN
  1848. result := SyntaxTree.invalidExpression
  1849. END;
  1850. elements.SetExpression(i,element);
  1851. END;
  1852. END;
  1853. IF constant THEN
  1854. value := Global.NewSetValue(system,set.position,s);
  1855. result.SetResolved(value);
  1856. result.SetType(value.type);
  1857. ELSE
  1858. result.SetType(system.setType);
  1859. END;
  1860. (* optimization possible
  1861. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1862. left this to the programmer...
  1863. *)
  1864. resolvedExpression := result;
  1865. END VisitSet;
  1866. (*
  1867. old variant: quite generic but needs better conversion handling, do this?
  1868. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1869. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1870. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1871. BEGIN
  1872. type := NIL;
  1873. numberElements := x.elements.Length();
  1874. FOR i := 0 TO numberElements-1 DO
  1875. expression := x.elements.GetExpression(i);
  1876. position := expression.position;
  1877. expression := ResolveExpression(x.elements.GetExpression(i));
  1878. x.elements.SetExpression(i,de);
  1879. IF type = NIL THEN
  1880. type := expression.type;
  1881. ELSIF CompatibleTo(system,expression.type,type) THEN
  1882. (* ok *)
  1883. ELSIF CompatibleTo(system,type,expression.type) THEN
  1884. type := expression.type
  1885. ELSE
  1886. Error(expression.position, "incompatible element types");
  1887. type := SyntaxTree.invalidType;
  1888. END;
  1889. END;
  1890. isValue := TRUE;
  1891. FOR i := 0 TO numberElements-1 DO
  1892. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1893. x.elements.SetExpression(i,expression);
  1894. isValue := isValue & (expression.resolved # NIL);
  1895. END;
  1896. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1897. arrayType.SetArrayBase(type);
  1898. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1899. IF isValue THEN
  1900. value := SyntaxTree.NewMathArrayValue(position);
  1901. value.SetElements(x.elements);
  1902. x.SetResolved(value);
  1903. END;
  1904. x.SetType(arrayType);
  1905. resolvedExpression := x;
  1906. END VisitMathArrayExpression;
  1907. *)
  1908. PROCEDURE VisitMathArrayExpression*(x: SyntaxTree.MathArrayExpression);
  1909. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1910. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1911. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1912. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1913. BEGIN
  1914. numberElements := x.elements.Length();
  1915. FOR i := 0 TO numberElements-1 DO
  1916. expression := x.elements.GetExpression(i);
  1917. IF expression IS SyntaxTree.MathArrayExpression THEN
  1918. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1919. ELSE
  1920. position := expression.position;
  1921. expression := ResolveExpression(x.elements.GetExpression(i));
  1922. x.elements.SetExpression(i,expression);
  1923. IF type = NIL THEN
  1924. type := expression.type;
  1925. ELSIF CompatibleTo(system,expression.type,type) THEN
  1926. (* ok *)
  1927. ELSIF CompatibleTo(system,type,expression.type) THEN
  1928. type := expression.type
  1929. ELSE
  1930. Error(expression.position, "incompatible element types");
  1931. type := SyntaxTree.invalidType;
  1932. END;
  1933. END;
  1934. END;
  1935. END RecursivelyFindType;
  1936. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1937. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1938. BEGIN
  1939. numberElements := x.elements.Length();
  1940. FOR i := 0 TO numberElements-1 DO
  1941. expression := x.elements.GetExpression(i);
  1942. IF expression IS SyntaxTree.MathArrayExpression THEN
  1943. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1944. ELSE
  1945. position := expression.position;
  1946. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1947. x.elements.SetExpression(i,expression);
  1948. isValue := isValue & (expression.resolved # NIL);
  1949. END;
  1950. END;
  1951. END RecursivelySetExpression;
  1952. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1953. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1954. arrayType: SyntaxTree.MathArrayType;
  1955. BEGIN
  1956. numberElements := x.elements.Length();
  1957. baseType := NIL;
  1958. gsize := 0;
  1959. FOR i := 0 TO numberElements-1 DO
  1960. expression := x.elements.GetExpression(i);
  1961. IF expression IS SyntaxTree.MathArrayExpression THEN
  1962. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1963. IF i=0 THEN
  1964. gsize := size;
  1965. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1966. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1967. ELSE expression.SetType(baseType)
  1968. END;
  1969. ELSIF baseType = NIL THEN baseType := type;
  1970. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1971. END;
  1972. END;
  1973. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1974. arrayType.SetArrayBase(baseType);
  1975. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1976. RETURN ResolveType(arrayType);
  1977. END RecursivelySetType;
  1978. BEGIN
  1979. type := NIL;
  1980. RecursivelyFindType(x);
  1981. isValue := TRUE;
  1982. RecursivelySetExpression(x);
  1983. arrayType := RecursivelySetType(x);
  1984. x.SetType(arrayType);
  1985. IF isValue THEN
  1986. value := SyntaxTree.NewMathArrayValue(x.position);
  1987. value.SetArray(x);
  1988. x.SetResolved(value);
  1989. value.SetType(arrayType);
  1990. END;
  1991. x.SetType(arrayType);
  1992. resolvedExpression := x;
  1993. END VisitMathArrayExpression;
  1994. (** check and resolve unary expression **)
  1995. PROCEDURE VisitUnaryExpression*(unaryExpression: SyntaxTree.UnaryExpression);
  1996. VAR
  1997. left: SyntaxTree.Expression;
  1998. int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; operator: LONGINT;
  1999. bool: BOOLEAN;
  2000. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  2001. value: SyntaxTree.Value;
  2002. BEGIN
  2003. type := SyntaxTree.invalidType;
  2004. left := ResolveExpression(unaryExpression.left);
  2005. unaryExpression.SetLeft(left);
  2006. operator := unaryExpression.operator;
  2007. result := unaryExpression;
  2008. IF ~system.operatorDefined[operator] THEN
  2009. Error(left.position,"Operator Not Defined");
  2010. RETURN
  2011. ELSIF left.type = NIL THEN
  2012. Error(left.position,"Invalid Nil Argument in Unary Expression");
  2013. resolvedExpression := SyntaxTree.invalidExpression;
  2014. RETURN
  2015. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  2016. RETURN
  2017. END;
  2018. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  2019. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  2020. END;
  2021. IF operatorCall # NIL THEN
  2022. result := operatorCall;
  2023. type := operatorCall.type;
  2024. (* admissible operators
  2025. Minus number, set
  2026. Not boolean
  2027. *)
  2028. ELSE
  2029. CASE unaryExpression.operator OF
  2030. |Scanner.Minus:
  2031. IF IsIntegerType(left.type.resolved) THEN
  2032. IF left.resolved # NIL THEN
  2033. int := -left.resolved(SyntaxTree.IntegerValue).value;
  2034. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2035. result.SetResolved(value);
  2036. type := Global.GetIntegerType(system,int);
  2037. value.SetType(type);
  2038. ELSE
  2039. type := left.type
  2040. END
  2041. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2042. IF IsRealValue(left,real) THEN
  2043. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2044. result.SetResolved(value);
  2045. type := left.type;
  2046. value.SetType(type);
  2047. ELSE
  2048. type := left.type;
  2049. END;
  2050. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2051. IF IsSetValue(left,set) THEN
  2052. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2053. result.SetResolved(value);
  2054. type := Global.GetSetType(system,-set);
  2055. value.SetType(type);
  2056. ELSE
  2057. type := left.type;
  2058. END;
  2059. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2060. IF IsComplexValue(left, real, imaginary) THEN
  2061. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2062. result.SetResolved(value);
  2063. type := left.type;
  2064. value.SetType(type);
  2065. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2066. ELSE
  2067. type := left.type;
  2068. END
  2069. ELSE
  2070. Error(left.position,"unary operator not applicable");
  2071. END;
  2072. |Scanner.Not:
  2073. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2074. IF IsBooleanValue(left,bool) THEN
  2075. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2076. result.SetResolved(value);
  2077. type := system.booleanType;
  2078. value.SetType(type);
  2079. ELSE
  2080. type := system.booleanType;
  2081. END;
  2082. ELSE
  2083. Error(left.position,"unary operator not applicable");
  2084. END;
  2085. |Scanner.Plus:
  2086. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2087. result := left; type := left.type;
  2088. ELSE
  2089. Error(left.position,"unary operator not applicable");
  2090. END;
  2091. (* ADDRESS OF *)
  2092. |Scanner.Address:
  2093. IF HasAddress(left) THEN
  2094. type := system.addressType;
  2095. ELSE
  2096. type := SyntaxTree.invalidType;
  2097. Error(left.position,"has no address");
  2098. Printout.Info("par", left);
  2099. END;
  2100. (* SIZE OF *)
  2101. |Scanner.Size:
  2102. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2103. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2104. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2105. value := SyntaxTree.NewIntegerValue(left.position, int);
  2106. result.SetResolved(value);
  2107. type := Global.GetIntegerType(system,int);
  2108. value.SetType(type)
  2109. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2110. ELSE
  2111. (* for variables, system sizeof could represent the physically occupied size
  2112. determined via the type descriptor, implement that ? *)
  2113. Error(left.position,"is not a type symbol");
  2114. END
  2115. (* ALIAS OF *)
  2116. |Scanner.Alias:
  2117. type := left.type.resolved;
  2118. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2119. type := SyntaxTree.invalidType;
  2120. Error(left.position,"alias on non math array type");
  2121. END;
  2122. ELSE
  2123. Error(left.position,"unary operator not defined");
  2124. END;
  2125. END;
  2126. result.SetType(type);
  2127. resolvedExpression := result
  2128. END VisitUnaryExpression;
  2129. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2130. VAR
  2131. result: SyntaxTree.Expression;
  2132. array: SyntaxTree.MathArrayExpression;
  2133. value: SyntaxTree.MathArrayValue;
  2134. isValue: BOOLEAN;
  2135. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2136. BEGIN
  2137. type := type.resolved;
  2138. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2139. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2140. END;
  2141. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2142. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2143. END;
  2144. RETURN type
  2145. END BaseType;
  2146. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2147. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2148. BEGIN
  2149. numberElements := x.elements.Length();
  2150. FOR i := 0 TO numberElements-1 DO
  2151. expression := x.elements.GetExpression(i);
  2152. IF expression IS SyntaxTree.MathArrayExpression THEN
  2153. array := SyntaxTree.NewMathArrayExpression(position);
  2154. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2155. expression := array;
  2156. ELSE
  2157. position := expression.position;
  2158. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2159. isValue := isValue & (expression.resolved # NIL);
  2160. END;
  2161. to.elements.AddExpression(expression);
  2162. END;
  2163. END RecursivelyConvert;
  2164. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2165. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2166. arrayType: SyntaxTree.MathArrayType;
  2167. BEGIN
  2168. numberElements := x.elements.Length();
  2169. baseType := NIL;
  2170. gsize := 0;
  2171. FOR i := 0 TO numberElements-1 DO
  2172. expression := x.elements.GetExpression(i);
  2173. IF expression IS SyntaxTree.MathArrayExpression THEN
  2174. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2175. IF i=0 THEN
  2176. gsize := size;
  2177. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2178. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2179. ELSE expression.SetType(baseType)
  2180. END;
  2181. ELSIF baseType = NIL THEN baseType := type;
  2182. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2183. END;
  2184. END;
  2185. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2186. arrayType.SetArrayBase(baseType);
  2187. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2188. RETURN ResolveType(arrayType);
  2189. END RecursivelySetType;
  2190. BEGIN
  2191. result := SyntaxTree.invalidExpression;
  2192. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2193. result := expression (* do not convert *)
  2194. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2195. isValue := TRUE;
  2196. type := BaseType(type);
  2197. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2198. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2199. value := SyntaxTree.NewMathArrayValue(array.position);
  2200. value.SetArray(array);
  2201. value.SetType(RecursivelySetType(array));
  2202. result := value;
  2203. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2204. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2205. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2206. IF result = NIL THEN
  2207. result := SyntaxTree.invalidExpression;
  2208. Error(position, "incompatible conversion");
  2209. IF VerboseErrorMessage THEN
  2210. Printout.Info("expression",expression);
  2211. Printout.Info("type",type);
  2212. END;
  2213. END;
  2214. END;
  2215. RETURN result
  2216. END MathArrayConversion;
  2217. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2218. VAR result: SyntaxTree.Expression; int: Basic.Integer; real, imaginary: LONGREAL; set: Basic.Set; char: CHAR; string: Scanner.StringType;
  2219. BEGIN
  2220. result := expression; type := type.resolved;
  2221. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2222. (* skip, no conversion *)
  2223. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2224. int := expression(SyntaxTree.IntegerValue).value;
  2225. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2226. int := Global.ConvertSigned(int,system.SizeOf(type));
  2227. result := SyntaxTree.NewIntegerValue(position,int);
  2228. result.SetType(type);
  2229. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2230. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2231. result := SyntaxTree.NewIntegerValue(position,int);
  2232. result.SetType(type);
  2233. ELSIF (type IS SyntaxTree.FloatType) THEN
  2234. result := SyntaxTree.NewRealValue(expression.position,int);
  2235. result.SetType(type);
  2236. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2237. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2238. result.SetType(type);
  2239. ELSIF (type IS SyntaxTree.SetType) THEN
  2240. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,int));
  2241. result.SetType(type);
  2242. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2243. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2244. result.SetType(type);
  2245. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2246. result := SyntaxTree.NewEnumerationValue(expression.position,int);
  2247. result.SetType(type);
  2248. ELSIF (type IS SyntaxTree.PortType) THEN
  2249. result := ConvertValue(position, expression, system.integerType);
  2250. ELSE
  2251. Error(position, "integer value cannot be converted");
  2252. result := SyntaxTree.invalidExpression;
  2253. IF VerboseErrorMessage THEN
  2254. Printout.Info("expression",expression);
  2255. Printout.Info("type",type);
  2256. END;
  2257. END;
  2258. ELSIF IsRealValue(expression,real) THEN
  2259. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2260. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2261. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2262. result.SetType(type);
  2263. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2264. int := ENTIERH(real);
  2265. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2266. result.SetType(type);
  2267. ELSIF (type IS SyntaxTree.FloatType) THEN
  2268. result := SyntaxTree.NewRealValue(position,real);
  2269. result.SetType(type);
  2270. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2271. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2272. result.SetType(type);
  2273. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2274. ELSIF (type IS SyntaxTree.PortType) THEN
  2275. result := ConvertValue(position, expression, system.integerType);
  2276. ELSE
  2277. Error(position, "real value cannot be converted");
  2278. result := SyntaxTree.invalidExpression;
  2279. END
  2280. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2281. IF (type IS SyntaxTree.ComplexType) THEN
  2282. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2283. result.SetType(type);
  2284. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2285. ELSE
  2286. Error(position, "complex value cannot be converted");
  2287. result := SyntaxTree.invalidExpression;
  2288. END
  2289. ELSIF IsSetValue(expression,set) THEN
  2290. IF (type IS SyntaxTree.IntegerType) THEN
  2291. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,set));
  2292. result.SetType(type);
  2293. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(Basic.Integer,set)) *)
  2294. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2295. result.SetType(type);
  2296. ELSIF (type IS SyntaxTree.PortType) THEN
  2297. result := ConvertValue(position, expression, system.integerType);
  2298. ELSIF (type IS SyntaxTree.SetType) THEN
  2299. set := Global.ConvertSet(set,system.SizeOf(type));
  2300. result := SyntaxTree.NewSetValue(expression.position,set);
  2301. result.SetType(type);
  2302. ELSE
  2303. Error(position, "set value cannot be converted");
  2304. result := SyntaxTree.invalidExpression;
  2305. END;
  2306. ELSIF IsStringValue(expression,string) THEN
  2307. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2308. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2309. result.SetType(type);
  2310. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2311. ELSE
  2312. Error(position, "string value cannot be converted");
  2313. result := SyntaxTree.invalidExpression;
  2314. END;
  2315. ELSIF IsCharacterValue(expression,char) THEN
  2316. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2317. string[0] := char; string[1] := 0X;
  2318. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2319. result := SyntaxTree.NewStringValue(expression.position,string);
  2320. result.SetType(type);
  2321. ELSIF (type IS SyntaxTree.ByteType) THEN
  2322. (* do not simply set the new type as this could invalidate types of constants *)
  2323. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2324. result.SetType(type)
  2325. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2326. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(Basic.Integer,char));
  2327. result.SetType(type);
  2328. ELSIF (type IS SyntaxTree.SetType) THEN
  2329. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(Basic.Set,Basic.Integer(ORD(char))));
  2330. result.SetType(type);
  2331. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2332. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2333. result.SetType(type);
  2334. ELSIF (type IS SyntaxTree.PortType) THEN
  2335. result := ConvertValue(position, expression, system.integerType);
  2336. ELSE
  2337. Error(position, "character value cannot be converted");
  2338. result := SyntaxTree.invalidExpression;
  2339. END;
  2340. ELSIF expression IS SyntaxTree.NilValue THEN
  2341. IF type IS SyntaxTree.AddressType THEN
  2342. result := SyntaxTree.NewIntegerValue(position,0);
  2343. result.SetType(type);
  2344. ELSE
  2345. result := expression;
  2346. END;
  2347. (* nothing to be done *)
  2348. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2349. result := MathArrayConversion(position, expression,type);
  2350. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2351. int := expression(SyntaxTree.EnumerationValue).value;
  2352. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2353. int := Global.ConvertSigned(int,system.SizeOf(type));
  2354. result := SyntaxTree.NewIntegerValue(position,int);
  2355. result.SetType(type);
  2356. ELSE
  2357. result := expression;
  2358. END;
  2359. (* nothing to be done *)
  2360. ELSE
  2361. Error(position, "expression cannot be converted");
  2362. IF VerboseErrorMessage THEN
  2363. Printout.Info("expression",expression);
  2364. Printout.Info("type",type);
  2365. END;
  2366. result := SyntaxTree.invalidExpression;
  2367. END;
  2368. RETURN result
  2369. END ConvertValue;
  2370. (**
  2371. return a conversion of an expression to a given type
  2372. - if expression is already of same type then return expression
  2373. - if incompatible conversion then report error and return invalidExpression
  2374. **)
  2375. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2376. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2377. BEGIN
  2378. type := type.resolved;
  2379. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2380. result := expression;
  2381. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2382. ELSIF expression = NIL THEN (* NIL expression *)
  2383. ELSIF expression.type = NIL THEN
  2384. Error(position, "expression of type NIL cannot be converted");
  2385. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2386. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2387. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2388. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2389. ELSIF expression.resolved # NIL THEN (* value *)
  2390. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2391. IF value IS SyntaxTree.Value THEN
  2392. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2393. result.SetResolved(value(SyntaxTree.Value));
  2394. result.SetType(value.type);
  2395. ELSE
  2396. result := value
  2397. END;
  2398. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2399. expressionList := SyntaxTree.NewExpressionList();
  2400. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2401. typeDeclaration.SetDeclaredType(type);
  2402. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2403. typeSymbol.SetType(typeDeclaration.type);
  2404. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2405. expressionList.AddExpression(expression);
  2406. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2407. result.SetType(type);
  2408. ELSIF IsArrayStructuredObjectType(type) THEN
  2409. (* no type can be converted to an array-structured object type *)
  2410. HALT(100)
  2411. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2412. IF inConversion>5 THEN
  2413. Error(expression.position,"recursive Conversion");
  2414. IF VerboseErrorMessage THEN
  2415. Printout.Info("expression",expression);
  2416. Printout.Info("type",type);
  2417. END;
  2418. END;
  2419. INC(inConversion);
  2420. IF inConversion < 10 THEN
  2421. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2422. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  2423. result := expression
  2424. ELSE
  2425. result := MathArrayConversion(position, expression,type);
  2426. END;
  2427. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2428. expression := ConvertToMathArray(expression);
  2429. type := MathArrayStructureOfType(type);
  2430. result := MathArrayConversion(position, expression, type)
  2431. ELSE
  2432. Error(expression.position,"cannot convert non array type to array type")
  2433. END;
  2434. END;
  2435. DEC(inConversion);
  2436. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2437. IF IsTensor(expression.type) & (IsUnsafePointer(type) OR (type IS SyntaxTree.AddressType)) THEN
  2438. result := expression; result.SetType(type);
  2439. ELSIF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2440. OR ~(type IS SyntaxTree.ArrayType) THEN
  2441. Error(expression.position,"cannot convert array type to non-array type")
  2442. END;
  2443. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2444. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2445. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2446. (*skip, no conversion*)
  2447. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2448. (* skip, no conversion *)
  2449. ELSE
  2450. ASSERT(~(type IS SyntaxTree.RangeType));
  2451. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2452. ASSERT(type # NIL);
  2453. END;
  2454. RETURN result
  2455. END NewConversion;
  2456. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2457. BEGIN
  2458. IF CompatibleTo(system,expression.type, type) THEN
  2459. RETURN NewConversion(position, expression, type, NIL);
  2460. ELSE
  2461. Error(expression.position, "incompatible expression");
  2462. RETURN SyntaxTree.invalidExpression
  2463. END;
  2464. END CompatibleConversion;
  2465. (**
  2466. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2467. **)
  2468. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2469. VAR leftType,rightType: SyntaxTree.Type;
  2470. BEGIN
  2471. IF left.type = NIL THEN Error(left.position,"no type")
  2472. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2473. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2474. (* error already handled *)
  2475. ELSE
  2476. leftType := left.type.resolved; rightType := right.type.resolved;
  2477. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2478. right := NewConversion(right.position, right, leftType, NIL);
  2479. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2480. left := NewConversion(left.position,left,rightType,NIL);
  2481. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2482. right := NewConversion(right.position, right, leftType, NIL);
  2483. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2484. left := NewConversion(left.position,left,rightType,NIL);
  2485. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2486. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2487. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2488. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2489. ELSIF
  2490. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2491. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2492. (* must be the case LONGREAL / COMPLEX ) *)
  2493. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2494. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2495. ELSE
  2496. Error(left.position,"incompatible operands");
  2497. END;
  2498. END;
  2499. END ConvertOperands;
  2500. (** find and return best operator matching to parameter list (nil, if none)
  2501. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2502. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2503. **)
  2504. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2505. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2506. identifier: SyntaxTree.Identifier;
  2507. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2508. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2509. BEGIN
  2510. operator := scope.firstOperator;
  2511. WHILE(operator # NIL) DO
  2512. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2513. procedureType := operator.type(SyntaxTree.ProcedureType);
  2514. distance := Distance(system, procedureType,actualParameters);
  2515. IF (distance < Infinity) THEN
  2516. IF returnType # NIL THEN
  2517. IF procedureType.returnType = NIL THEN
  2518. distance := Infinity
  2519. ELSE
  2520. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2521. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2522. END;
  2523. END;
  2524. END;
  2525. (*
  2526. IF distance < Infinity THEN
  2527. TRACE(distance, operator);
  2528. Printout.Info("potential operator",operator);
  2529. ELSE
  2530. Printout.Info("operator not possible",operator);
  2531. END;
  2532. *)
  2533. IF distance < bestDistance THEN
  2534. bestDistance := distance;
  2535. bestOperator := operator;
  2536. END;
  2537. END;
  2538. operator := operator.nextOperator;
  2539. END;
  2540. (*
  2541. Printout.Info("taken operator",bestOperator);
  2542. *)
  2543. END FindInScope;
  2544. BEGIN
  2545. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2546. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2547. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2548. import := currentScope.ownerModule.moduleScope.firstImport;
  2549. WHILE (bestDistance > 0) & (import # NIL) DO
  2550. IF import.module # NIL THEN
  2551. identifier := Global.GetIdentifier(operator,import.module.case);
  2552. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2553. END;
  2554. import := import.nextImport;
  2555. END;
  2556. RETURN bestOperator
  2557. END FindOperator;
  2558. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2559. BEGIN
  2560. currentScope := scope;
  2561. END SetCurrentScope;
  2562. (**
  2563. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2564. - handle LEN and DIM operator for array-structured object types
  2565. - find operator, if found then
  2566. - if in other module then add import designator
  2567. - create symbol designator for operator
  2568. - if error then return invalidExpression, if no operator then return NIL
  2569. **)
  2570. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2571. VAR
  2572. operator: SyntaxTree.Operator;
  2573. import: SyntaxTree.Import;
  2574. expression, result: SyntaxTree.Expression;
  2575. designator: SyntaxTree.Designator;
  2576. actualParameters, tempList: SyntaxTree.ExpressionList;
  2577. recordType: SyntaxTree.RecordType;
  2578. castReturnType : SyntaxTree.MathArrayType;
  2579. BEGIN
  2580. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2581. result := SyntaxTree.invalidExpression
  2582. ELSIF leftExpression = NIL THEN
  2583. result := NIL
  2584. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2585. (* LEN or DIM operator on array-structured object type *)
  2586. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2587. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2588. IF recordType.arrayAccessOperators.len = NIL THEN
  2589. Error(position, "call of undeclared LEN operator");
  2590. result := SyntaxTree.invalidExpression
  2591. ELSE
  2592. ASSERT(leftExpression IS SyntaxTree.Designator);
  2593. designator := leftExpression(SyntaxTree.Designator);
  2594. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2595. ASSERT(expression IS SyntaxTree.Designator);
  2596. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2597. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2598. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2599. result := designator
  2600. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2601. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2602. tempList := SyntaxTree.NewExpressionList();
  2603. tempList.AddExpression(rightExpression);
  2604. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2605. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2606. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2607. tempList := SyntaxTree.NewExpressionList();
  2608. tempList.AddExpression(designator);
  2609. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2610. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2611. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2612. END
  2613. END;
  2614. ELSE
  2615. IF ~complexNumbersImported THEN
  2616. IF (leftExpression # NIL) & IsComplexType(leftExpression.type)
  2617. OR (rightExpression # NIL) & IsComplexType(rightExpression.type)
  2618. THEN
  2619. (* operators on complex numbers *)
  2620. ImportModule(Global.ComplexNumbersName,position);
  2621. complexNumbersImported := TRUE;
  2622. END;
  2623. END;
  2624. (* import OCArrayBase if needed *)
  2625. IF ~arrayBaseImported THEN
  2626. IF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2627. IF op = Global.Dim THEN
  2628. (* not existing in OCArrayBase *)
  2629. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2630. (* not existing in OCArrayBase *)
  2631. ELSE
  2632. ImportModule(Global.ArrayBaseName,position);
  2633. arrayBaseImported := TRUE;
  2634. END
  2635. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2636. ImportModule(Global.ArrayBaseName,position);
  2637. arrayBaseImported := TRUE
  2638. END;
  2639. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2640. (* LEN(RANGE) *)
  2641. ImportModule(Global.ArrayBaseName,position);
  2642. arrayBaseImported := TRUE;
  2643. END;
  2644. END;
  2645. actualParameters := SyntaxTree.NewExpressionList();
  2646. actualParameters.AddExpression(leftExpression);
  2647. IF rightExpression # NIL THEN
  2648. actualParameters.AddExpression(rightExpression)
  2649. END;
  2650. operator := FindOperator(system,op,actualParameters,resultType);
  2651. IF operator # NIL THEN
  2652. designator := NIL;
  2653. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2654. import := currentScope.ownerModule.moduleScope.firstImport;
  2655. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2656. import := import.nextImport;
  2657. END;
  2658. expression := NewSymbolDesignator(position,NIL,import);
  2659. designator := expression(SyntaxTree.Designator);
  2660. END;
  2661. expression := NewSymbolDesignator(position,designator,operator);
  2662. designator := expression(SyntaxTree.Designator);
  2663. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2664. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2665. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2666. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2667. result.SetType(castReturnType);
  2668. END;
  2669. ELSE
  2670. result := NIL;
  2671. END;
  2672. END;
  2673. RETURN result
  2674. END NewOperatorCall;
  2675. (** check and resolve binary expression **)
  2676. (*! clean up *)
  2677. PROCEDURE VisitBinaryExpression*(binaryExpression: SyntaxTree.BinaryExpression);
  2678. VAR left,right,result: SyntaxTree.Expression;
  2679. leftType, rightType: SyntaxTree.Type;
  2680. il,ir: Basic.Integer; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: Basic.Integer;bl,br: BOOLEAN; sl,sr: Basic.Set; strl,strr: Scanner.StringType;
  2681. cl,cr: CHAR;
  2682. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2683. type: SyntaxTree.Type;
  2684. value: SyntaxTree.Value;
  2685. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: Basic.Integer;
  2686. integerConstantFolding: BOOLEAN;
  2687. list: SyntaxTree.ExpressionList;
  2688. PROCEDURE NewBool(v: BOOLEAN);
  2689. BEGIN
  2690. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2691. value.SetType(system.booleanType);
  2692. result.SetResolved(value);
  2693. type := system.booleanType
  2694. END NewBool;
  2695. PROCEDURE NewSet(v: Basic.Set);
  2696. BEGIN
  2697. value := Global.NewSetValue(system,binaryExpression.position,v);
  2698. result.SetResolved(value);
  2699. type := value.type;
  2700. END NewSet;
  2701. PROCEDURE NewInteger(v: Basic.Integer; t: SyntaxTree.Type);
  2702. BEGIN
  2703. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2704. (* type cast to "larger" type only if the value is still in the range *)
  2705. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2706. value.SetType(t);
  2707. END;
  2708. result.SetResolved(value);
  2709. type := value.type;
  2710. END NewInteger;
  2711. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2712. BEGIN
  2713. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2714. value.SetType(t);
  2715. result.SetResolved(value);
  2716. type := t;
  2717. END NewReal;
  2718. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2719. BEGIN
  2720. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2721. value.SetType(t);
  2722. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2723. result.SetResolved(value);
  2724. type := t;
  2725. END NewComplex;
  2726. BEGIN
  2727. type := SyntaxTree.invalidType;
  2728. left := ResolveExpression(binaryExpression.left);
  2729. right := ResolveExpression(binaryExpression.right);
  2730. binaryExpression.SetLeft(left);
  2731. binaryExpression.SetRight(right);
  2732. result := binaryExpression;
  2733. operator := binaryExpression.operator;
  2734. IF ~system.operatorDefined[operator] THEN
  2735. Error(left.position,"Operator Not Defined");
  2736. result := SyntaxTree.invalidExpression;
  2737. RETURN
  2738. END;
  2739. IF left.type = NIL THEN
  2740. Error(left.position,"Expression has no result type");
  2741. result := SyntaxTree.invalidExpression;
  2742. RETURN;
  2743. END;
  2744. IF right.type = NIL THEN
  2745. Error(right.position,"Expression has no result type");
  2746. result := SyntaxTree.invalidExpression;
  2747. RETURN;
  2748. END;
  2749. leftType := left.type.resolved; rightType := right.type.resolved;
  2750. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2751. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2752. END;
  2753. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2754. list := SyntaxTree.NewExpressionList();
  2755. list.AddExpression(right);
  2756. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2757. END;
  2758. IF operatorCall # NIL THEN
  2759. result := operatorCall;
  2760. type := operatorCall.type;
  2761. (* admissible operators:
  2762. Times, Plus, Minus numeric numeric numeric
  2763. set set set
  2764. Slash numeric numeric real /complex
  2765. set set set
  2766. Div , Mod integer integer integer
  2767. And, Or bool bool bool
  2768. Equal, Unequal basic basic bool
  2769. pointer pointer bool
  2770. object object bool
  2771. record record bool
  2772. string string bool
  2773. enumerator enumerator bool
  2774. Less, LessEqual,
  2775. Greater, GreaterEqual integer/real integer/real bool
  2776. enumerator enumerator bool
  2777. In integer set bool
  2778. Is pointer type bool
  2779. object type bool
  2780. record type bool
  2781. Upto: special abbreviation for a..b
  2782. *)
  2783. ELSIF (left.type = NIL) THEN
  2784. Error(left.position,"type (left operand) = NIL in binary expression");
  2785. D.Str("nil type in "); D.Type(left); D.Ln;
  2786. result := SyntaxTree.invalidExpression;
  2787. ELSIF (right.type = NIL) THEN
  2788. Error(right.position,"type (right operand) = NIL in binary expression");
  2789. result := SyntaxTree.invalidExpression;
  2790. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2791. result := SyntaxTree.invalidExpression;
  2792. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2793. HALT(100);
  2794. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2795. type := system.booleanType;
  2796. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2797. Error(right.position,"is not a type ");
  2798. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2799. Error(binaryExpression.position,"is not a type extension of ");
  2800. IF VerboseErrorMessage THEN
  2801. Printout.Info("left",left);
  2802. Printout.Info("right",right);
  2803. END;
  2804. ELSIF IsUnsafePointer(left.type) THEN
  2805. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2806. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2807. NewBool(TRUE)
  2808. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2809. NewBool(TRUE);
  2810. ELSIF IsUnextensibleRecord(left) THEN
  2811. NewBool(FALSE)
  2812. END
  2813. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2814. Error(right.position,"must not be a type");
  2815. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2816. Error(left.position,"must not be a type");
  2817. ELSIF operator = Scanner.In THEN (* left IN right *)
  2818. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2819. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2820. NewBool(il IN sr);
  2821. ELSE
  2822. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2823. left := NewConversion(left.position, left, system.longintType,NIL);
  2824. binaryExpression.SetLeft(left)
  2825. END;
  2826. type := system.booleanType;
  2827. END
  2828. ELSE
  2829. Error(binaryExpression.position, "incompatible operands");
  2830. END
  2831. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2832. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2833. Error(binaryExpression.position,"incompatible operands");
  2834. END;
  2835. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2836. ELSE Error(binaryExpression.position,"operator not defined 1")
  2837. END
  2838. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2839. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2840. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2841. Error(binaryExpression.position,"incompatible operands");
  2842. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2843. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) THEN
  2844. left := NewConversion(left.position, left, system.addressType, NIL);
  2845. right := NewConversion(right.position, right, system.addressType, NIL);
  2846. binaryExpression.SetLeft(left);
  2847. binaryExpression.SetRight(right);
  2848. type := system.addressType;
  2849. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2850. ConvertOperands(left, right);
  2851. binaryExpression.SetLeft(left);
  2852. binaryExpression.SetRight(right);
  2853. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2854. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2855. END;
  2856. type := system.booleanType;
  2857. ELSE
  2858. Error(binaryExpression.position,"operator not defined 3");
  2859. END
  2860. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2861. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2862. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2863. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2864. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2865. CASE operator OF
  2866. |Scanner.Equal: NewBool(strl^=strr^);
  2867. |Scanner.Unequal:NewBool(strl^#strr^);
  2868. |Scanner.Less: NewBool(strl^<strr^);
  2869. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2870. |Scanner.Greater: NewBool(strl^>strr^);
  2871. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2872. ELSE
  2873. Error(binaryExpression.position,"operator not defined 4");
  2874. END;
  2875. END;
  2876. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2877. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2878. type := system.booleanType
  2879. ELSE
  2880. Error(binaryExpression.position,"operator not defined 5");
  2881. END;
  2882. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2883. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2884. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2885. type := system.booleanType;
  2886. ELSE
  2887. Error(binaryExpression.position,"operator not defined 6");
  2888. END
  2889. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2890. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2891. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2892. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2893. type := system.booleanType
  2894. ELSE
  2895. Error(binaryExpression.position,"operator not defined for enumerators");
  2896. END;
  2897. ELSE
  2898. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2899. END;
  2900. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2901. type := system.booleanType;
  2902. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2903. type := system.booleanType;
  2904. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2905. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2906. THEN
  2907. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2908. IF (leftType # rightType) THEN
  2909. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2910. ConvertOperands(left,right); (* operands must be of the same type here *)
  2911. END;
  2912. binaryExpression.SetLeft(left);
  2913. binaryExpression.SetRight(right);
  2914. leftType := left.type.resolved;
  2915. rightType := right.type.resolved;
  2916. END;
  2917. type := leftType;
  2918. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2919. Error(binaryExpression.position,"conversion failed ?");
  2920. IF VerboseErrorMessage THEN
  2921. Printout.Info("left",left);
  2922. Printout.Info("right",right);
  2923. END;
  2924. ELSIF IsIntegerType(leftType) THEN
  2925. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2926. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2927. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2928. Error(binaryExpression.position,"division by zero");
  2929. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2930. Error(binaryExpression.position,"integer division by negative number");
  2931. END;
  2932. END;
  2933. (* constant folding *)
  2934. (* bootstrap64
  2935. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2936. CASE operator OF
  2937. |Scanner.Plus: NewInteger(il+ir,left.type);
  2938. |Scanner.Minus: NewInteger(il-ir,left.type);
  2939. |Scanner.Times: NewInteger(il*ir,left.type);
  2940. |Scanner.Slash:
  2941. IF ir # 0 THEN
  2942. NewReal(il/ir, system.realType);
  2943. END;
  2944. |Scanner.Mod:
  2945. IF ir > 0 THEN
  2946. NewInteger(il MOD ir,left.type);
  2947. END;
  2948. |Scanner.Div:
  2949. IF ir > 0 THEN
  2950. NewInteger(il DIV ir,left.type);
  2951. END;
  2952. |Scanner.Equal: NewBool(il=ir);
  2953. |Scanner.Unequal:NewBool(il#ir);
  2954. |Scanner.Less: NewBool(il<ir);
  2955. |Scanner.LessEqual: NewBool(il<=ir);
  2956. |Scanner.Greater: NewBool(il>ir);
  2957. |Scanner.GreaterEqual: NewBool(il>=ir);
  2958. ELSE Error(binaryExpression.position,"operator not defined 7");
  2959. END;
  2960. ELS*)
  2961. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2962. hl := left.resolved(SyntaxTree.IntegerValue).value;
  2963. hr := right.resolved(SyntaxTree.IntegerValue).value;
  2964. CASE operator OF
  2965. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2966. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2967. |Scanner.Times: NewInteger(hl*hr,left.type);
  2968. |Scanner.Slash:
  2969. IF hr = 0 THEN
  2970. Error(binaryExpression.position,"division by zero");
  2971. ELSE
  2972. IF type.sizeInBits = 64 THEN
  2973. NewReal(hl/hr,system.longrealType);
  2974. ELSE
  2975. NewReal(hl/hr,system.realType)
  2976. END
  2977. END;
  2978. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2979. |Scanner.Mod:
  2980. IF hr = 0 THEN
  2981. Error(binaryExpression.position,"division by zero");
  2982. ELSE
  2983. NewInteger(hl MOD hr, left.type);
  2984. (* bootstrap64
  2985. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2986. *)
  2987. END;
  2988. |Scanner.Div:
  2989. IF hr = 0 THEN
  2990. Error(binaryExpression.position,"division by zero");
  2991. ELSE
  2992. NewInteger(hl DIV hr, left.type);
  2993. (* bootstrap64
  2994. NewInteger(Machine.DivH(hl,hr),left.type);
  2995. *)
  2996. END;
  2997. (* *)
  2998. |Scanner.Equal: NewBool(hl=hr);
  2999. |Scanner.Unequal: NewBool(hl#hr);
  3000. |Scanner.Less: NewBool(hl<hr);
  3001. |Scanner.LessEqual: NewBool(hl<=hr);
  3002. |Scanner.Greater: NewBool(hl>hr);
  3003. |Scanner.GreaterEqual:NewBool(hl>=hr);
  3004. ELSE Error(binaryExpression.position,"operator not defined 8");
  3005. END;
  3006. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  3007. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  3008. type := left.type
  3009. ELSIF (operator = Scanner.Slash) THEN
  3010. left := NewConversion(left.position,left,system.realType,NIL);
  3011. right := NewConversion(right.position,right,system.realType,NIL);
  3012. binaryExpression.SetLeft(left);
  3013. binaryExpression.SetRight(right);
  3014. type := system.realType
  3015. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3016. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3017. type := system.booleanType
  3018. ELSE
  3019. Error(binaryExpression.position,"operator not defined 9");
  3020. END;
  3021. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  3022. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  3023. CASE operator OF
  3024. |Scanner.Plus: NewReal(rl+rr,leftType);
  3025. |Scanner.Minus: NewReal(rl-rr,leftType);
  3026. |Scanner.Times:NewReal(rl*rr,leftType);
  3027. |Scanner.Slash:
  3028. IF rr = 0 THEN
  3029. Error(binaryExpression.position,"division by zero");
  3030. ELSE
  3031. NewReal(rl/rr,leftType);
  3032. END
  3033. |Scanner.Equal: NewBool(rl=rr);
  3034. |Scanner.Unequal: NewBool(rl#rr);
  3035. |Scanner.Less: NewBool(rl<rr);
  3036. |Scanner.LessEqual: NewBool(rl<=rr);
  3037. |Scanner.Greater: NewBool(rl>rr);
  3038. |Scanner.GreaterEqual: NewBool(rl>=rr);
  3039. ELSE Error(binaryExpression.position,"operator not defined 10");
  3040. END;
  3041. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3042. type := left.type
  3043. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3044. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3045. type := system.booleanType
  3046. ELSE
  3047. Error(binaryExpression.position,"operator not defined 11");
  3048. IF VerboseErrorMessage THEN
  3049. Printout.Info("left",left);
  3050. Printout.Info("right",right);
  3051. END;
  3052. END;
  3053. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3054. CASE operator OF
  3055. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3056. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3057. ELSE
  3058. Error(binaryExpression.position,"operator not defined");
  3059. IF VerboseErrorMessage THEN
  3060. Printout.Info("left", left);
  3061. Printout.Info("right", right)
  3062. END;
  3063. END;
  3064. IF ~error THEN
  3065. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3066. Error(binaryExpression.position,"division by zero")
  3067. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3068. (* do constant folding *)
  3069. CASE operator OF
  3070. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3071. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3072. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3073. |Scanner.Slash:
  3074. divisor := c * c + d * d;
  3075. ASSERT(divisor # 0);
  3076. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3077. |Scanner.Equal: NewBool((a = c) & (b = d))
  3078. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3079. END
  3080. END
  3081. END
  3082. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3083. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3084. CASE operator OF
  3085. |Scanner.And: NewBool(bl & br);
  3086. |Scanner.Or: NewBool(bl OR br);
  3087. |Scanner.Equal: NewBool(bl = br);
  3088. |Scanner.Unequal: NewBool(bl # br);
  3089. ELSE Error(binaryExpression.position,"operator not defined 12");
  3090. END;
  3091. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3092. type := system.booleanType
  3093. ELSE
  3094. Error(binaryExpression.position,"operator not defined 13");
  3095. END;
  3096. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3097. (* constant folding *)
  3098. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3099. IF operator = Scanner.Equal THEN
  3100. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3101. ELSIF operator = Scanner.Unequal THEN
  3102. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3103. END;
  3104. END;
  3105. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3106. type := system.booleanType;
  3107. ELSE
  3108. Error(binaryExpression.position, "operator not defined");
  3109. END;
  3110. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3111. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3112. CASE operator OF
  3113. |Scanner.Plus: NewSet(sl + sr);
  3114. |Scanner.Minus: NewSet(sl - sr);
  3115. |Scanner.Times: NewSet(sl * sr);
  3116. |Scanner.Slash: NewSet(sl / sr);
  3117. |Scanner.Equal: NewBool(sl=sr);
  3118. |Scanner.Unequal: NewBool(sl#sr);
  3119. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3120. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3121. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3122. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3123. ELSE Error(binaryExpression.position,"operator not defined 14");
  3124. END;
  3125. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3126. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3127. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3128. THEN
  3129. type := system.booleanType
  3130. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3131. type := left.type
  3132. ELSE
  3133. Error(binaryExpression.position,"operator not defined 15");
  3134. END;
  3135. ELSIF IsCharacterType(left.type) THEN
  3136. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3137. CASE operator OF
  3138. |Scanner.Equal: NewBool(cl=cr);
  3139. |Scanner.Unequal: NewBool(cl#cr);
  3140. |Scanner.Less: NewBool(cl<cr);
  3141. |Scanner.LessEqual: NewBool(cl<=cr);
  3142. |Scanner.Greater: NewBool(cl>cr);
  3143. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3144. ELSE Error(binaryExpression.position,"operator not defined 16");
  3145. END;
  3146. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3147. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3148. type := system.booleanType
  3149. ELSE
  3150. Error(binaryExpression.position,"operator not defined 17");
  3151. END;
  3152. ELSE
  3153. Error(binaryExpression.position,"operator not defined 18");
  3154. END;
  3155. ELSE
  3156. Error(binaryExpression.position,"operator not defined 19");
  3157. END;
  3158. IF type = SyntaxTree.invalidType THEN
  3159. result := SyntaxTree.invalidExpression
  3160. ELSE
  3161. result.SetType(type)
  3162. END;
  3163. resolvedExpression := result
  3164. END VisitBinaryExpression;
  3165. (** resolve a range expression of the from <<first .. last BY step>>
  3166. - depending on the context different things are checked:
  3167. ArrayIndex:
  3168. - components must be integers
  3169. - replace missing lower bound with 0
  3170. - replace missing upper bound with MAX(LONGINT)
  3171. - replace missing step size with 1
  3172. SetElement:
  3173. - components must be integers
  3174. - replace missing lower bound with 0
  3175. - replace missing upper bound with MAX(SET)
  3176. - must not have step size
  3177. CaseGuard:
  3178. - components must be constant
  3179. - components must be integers or characters
  3180. - must have lower and upper bound present
  3181. - components are made compatible
  3182. - must not have step size
  3183. - if error: return invalidExpression
  3184. **)
  3185. PROCEDURE VisitRangeExpression*(x: SyntaxTree.RangeExpression);
  3186. VAR
  3187. hasError: BOOLEAN;
  3188. first, last, step: SyntaxTree.Expression;
  3189. BEGIN
  3190. hasError := FALSE;
  3191. first := x.first;
  3192. last := x.last;
  3193. step := x.step;
  3194. (* check lower bound *)
  3195. IF x.context = SyntaxTree.CaseGuard THEN
  3196. IF first = NIL THEN
  3197. Error(x.position, "missing lower bound");
  3198. hasError := TRUE
  3199. ELSE
  3200. first := ResolveExpression(first);
  3201. IF ~IsIntegerType(first.type.resolved) & ~IsCharacterType(first.type.resolved) THEN
  3202. Error(first.position, "lower bound not integer or character");
  3203. hasError := TRUE
  3204. ELSE
  3205. IF first IS SyntaxTree.StringValue THEN
  3206. (* add conversion from string to character *)
  3207. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3208. END
  3209. END;
  3210. (* check if expression is constant *)
  3211. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3212. (* error already reported *)
  3213. hasError := TRUE
  3214. END
  3215. END
  3216. ELSE (* ArrayIndex, SetElement *)
  3217. IF first = NIL THEN
  3218. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3219. END;
  3220. first := ResolveExpression(first);
  3221. IF IsIntegerType(first.type.resolved) THEN
  3222. first := NewConversion(first.position, first, system.lenType, NIL)
  3223. ELSE
  3224. Error(first.position, "lower bound not integer");
  3225. hasError := TRUE
  3226. END
  3227. END;
  3228. (* check upper bound *)
  3229. IF x.context = SyntaxTree.CaseGuard THEN
  3230. IF last = NIL THEN
  3231. Error(x.position, "missing upper bound");
  3232. hasError := TRUE
  3233. ELSE
  3234. last := ResolveExpression(last);
  3235. IF ~IsIntegerType(last.type.resolved) & ~IsCharacterType(last.type.resolved) THEN
  3236. Error(last.position, "lower bound not integer or character");
  3237. hasError := TRUE
  3238. ELSE
  3239. IF last IS SyntaxTree.StringValue THEN
  3240. (* add conversion from string to character *)
  3241. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3242. END
  3243. END;
  3244. (* check if expression is constant *)
  3245. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3246. (* error already reported *)
  3247. hasError := TRUE
  3248. ELSE
  3249. (* try to make lower and upper bound compatible *)
  3250. ConvertOperands(first, last);
  3251. IF first.type.resolved # last.type.resolved THEN
  3252. Error(x.position, "lower and upper bounds incompatible");
  3253. hasError := TRUE
  3254. END
  3255. END
  3256. END
  3257. ELSE (* ArrayIndex, SetElement *)
  3258. IF last = NIL THEN
  3259. IF x.context = SyntaxTree.ArrayIndex THEN
  3260. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3261. ELSE
  3262. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3263. END
  3264. END;
  3265. last := ResolveExpression(last);
  3266. IF IsIntegerType(last.type.resolved) THEN
  3267. last := NewConversion(last.position, last, system.lenType, NIL)
  3268. ELSE
  3269. Error(last.position, "upper bound not integer");
  3270. hasError := TRUE
  3271. END
  3272. END;
  3273. (* check step size *)
  3274. IF x.context = SyntaxTree.ArrayIndex THEN
  3275. IF step = NIL THEN
  3276. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3277. END;
  3278. step := ResolveExpression(step);
  3279. IF IsIntegerType(step.type.resolved) THEN
  3280. step := NewConversion(step.position, step, system.lenType, NIL)
  3281. ELSE
  3282. Error(step.position, "step size not integer");
  3283. hasError := TRUE
  3284. END
  3285. ELSE (* SetElement, CaseGuard *)
  3286. IF step # NIL THEN
  3287. Error(last.position, "step size not allowed in this context");
  3288. hasError := TRUE
  3289. END
  3290. END;
  3291. IF hasError THEN
  3292. resolvedExpression := SyntaxTree.invalidExpression
  3293. ELSE
  3294. x.SetFirst(first);
  3295. x.SetLast(last);
  3296. x.SetStep(step);
  3297. x.SetType(system.rangeType);
  3298. resolvedExpression := x;
  3299. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3300. END
  3301. END VisitRangeExpression;
  3302. PROCEDURE VisitTensorRangeExpression*(x: SyntaxTree.TensorRangeExpression);
  3303. BEGIN
  3304. x.SetType(NIL);
  3305. resolvedExpression := x;
  3306. END VisitTensorRangeExpression;
  3307. (** resolve the expression d and return result as designator
  3308. - resolve expression
  3309. - if expression is a designator then return designator else error message and return invalidDesignator
  3310. **)
  3311. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3312. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3313. BEGIN
  3314. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3315. resolved := ResolveExpression(d);
  3316. IF resolved = SyntaxTree.invalidExpression THEN
  3317. (* error should already have been reported *)
  3318. result := SyntaxTree.invalidDesignator;
  3319. ELSIF resolved IS SyntaxTree.Designator THEN
  3320. result := resolved(SyntaxTree.Designator);
  3321. ELSE
  3322. Error(d.position,"is no designator ! ");
  3323. result := SyntaxTree.invalidDesignator;
  3324. END;
  3325. (* result.type might be nil. *)
  3326. RETURN result
  3327. END ResolveDesignator;
  3328. (**
  3329. symbol designator generated in this module
  3330. nothing to be resolved
  3331. **)
  3332. PROCEDURE VisitSymbolDesignator*(x: SyntaxTree.SymbolDesignator);
  3333. BEGIN
  3334. resolvedExpression := x;
  3335. END VisitSymbolDesignator;
  3336. (**
  3337. self designator generated in this module
  3338. nothing to be resolved
  3339. **)
  3340. PROCEDURE VisitSelfDesignator*(x: SyntaxTree.SelfDesignator);
  3341. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3342. BEGIN
  3343. (* check if in record scope *)
  3344. scope := currentScope;
  3345. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).selfParameter # NIL) THEN
  3346. resolvedExpression :=
  3347. NewSymbolDesignator(x.position, NIL, scope(SyntaxTree.ProcedureScope).ownerProcedure.type(SyntaxTree.ProcedureType).selfParameter); RETURN;
  3348. END;
  3349. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3350. scope := scope.outerScope;
  3351. END;
  3352. IF scope = NIL THEN (* in module scope *)
  3353. x.SetType(system.anyType);
  3354. ELSIF scope IS SyntaxTree.CellScope THEN
  3355. cell := scope(SyntaxTree.CellScope).ownerCell;
  3356. x.SetType(cell);
  3357. ELSE (* in record scope *)
  3358. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3359. IF (record # NIL) & (record.pointerType # NIL) THEN
  3360. type := ResolveType(record.pointerType);
  3361. x.SetType(type);
  3362. ELSE
  3363. x.SetType(record);
  3364. x.SetAssignable(TRUE); (* var parameter *)
  3365. END;
  3366. END;
  3367. resolvedExpression := x;
  3368. END VisitSelfDesignator;
  3369. PROCEDURE VisitResultDesignator*(x: SyntaxTree.ResultDesignator);
  3370. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3371. BEGIN
  3372. scope := currentScope;
  3373. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3374. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3375. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3376. returnType := procedureType.returnType;
  3377. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3378. THEN
  3379. x.SetType(returnType);
  3380. ELSE
  3381. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3382. x.SetType(SyntaxTree.invalidType);
  3383. END;
  3384. ELSE
  3385. Error(x.position,"forbidden access to result designator");
  3386. x.SetType(SyntaxTree.invalidType);
  3387. END;
  3388. x.SetAssignable(TRUE);
  3389. resolvedExpression := x;
  3390. END VisitResultDesignator;
  3391. (**
  3392. return symbol designator as an expression
  3393. - if symbol is a constant then return the constant value expression
  3394. - else
  3395. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3396. - if symbol is a guarded variable then return a TypeGuardDesignator
  3397. - else return a symbol designator
  3398. **)
  3399. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3400. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3401. guardType: SyntaxTree.Type;
  3402. BEGIN
  3403. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3404. result := SyntaxTree.invalidExpression;
  3405. ASSERT(symbol # NIL);
  3406. (*
  3407. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3408. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3409. Error(position, "type not allowed here");
  3410. ELS *)
  3411. (* not needed any more as values are stored in the expression
  3412. IF symbol IS SyntaxTree.Constant THEN
  3413. result := symbol(SyntaxTree.Constant).value
  3414. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3415. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3416. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3417. ELSE
  3418. result := symbol(SyntaxTree.Constant).value
  3419. END;
  3420. ELSE
  3421. *)
  3422. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3423. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3424. THEN
  3425. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3426. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3427. left := NewDereferenceDesignator(position,left);
  3428. left.SetHidden(TRUE);
  3429. END;
  3430. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3431. scope := currentScope;
  3432. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3433. scope := scope.outerScope;
  3434. END;
  3435. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3436. Error(position, "forbidden access to symbol in parent procedure scope");
  3437. END;
  3438. END;
  3439. 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);
  3440. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3441. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3442. ELSE
  3443. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3444. END;
  3445. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3446. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3447. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3448. result.SetType(symbol.type);
  3449. result.SetAssignable(assignable);
  3450. symbol.MarkUsed;
  3451. IF symbol IS SyntaxTree.Constant THEN
  3452. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3453. END;
  3454. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3455. variableAccessed := TRUE
  3456. END;
  3457. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3458. IF GetGuard(symbol,guardType) THEN
  3459. IF (symbol.type.resolved IS SyntaxTree.RecordType) OR ~assignable THEN (* type guard is tested and type cannot be changed *)
  3460. result.SetType(guardType);
  3461. ELSE
  3462. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3463. END;
  3464. END;
  3465. END;
  3466. ASSERT(result.type # NIL);
  3467. RETURN result
  3468. END NewSymbolDesignator;
  3469. (** check and resolve an identifier designator "identifier"
  3470. - if identifier = self then return SelfDesignator
  3471. - else find symbol in current scope
  3472. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3473. **)
  3474. PROCEDURE VisitIdentifierDesignator*(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3475. VAR symbol: SyntaxTree.Symbol;
  3476. BEGIN
  3477. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3478. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3479. IF symbol # NIL THEN
  3480. ResolveSymbol(symbol);
  3481. ASSERT(symbol.type # NIL);
  3482. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3483. ELSE
  3484. Error(identifierDesignator.position,"Undeclared Identifier");
  3485. IF VerboseErrorMessage THEN
  3486. Printout.Info("undeclared identifier designator",identifierDesignator);
  3487. END;
  3488. resolvedExpression := SyntaxTree.invalidDesignator;
  3489. END;
  3490. END VisitIdentifierDesignator;
  3491. (** check and resolve a selector designator of the form left.designator
  3492. - if left is a pointer type then do auto dereferenciation
  3493. - left denotes a search scope:
  3494. - if left type is import type then set search scope to respective module
  3495. - if left type is enumeration type then set search scope to respective enumeration scope
  3496. - elsif left type is record type then set search scope to record scope
  3497. - search symbol in computed scope
  3498. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3499. **)
  3500. PROCEDURE VisitSelectorDesignator*(selectorDesignator: SyntaxTree.SelectorDesignator);
  3501. VAR
  3502. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3503. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3504. BEGIN
  3505. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3506. left := ResolveDesignator(selectorDesignator.left);
  3507. result := SyntaxTree.invalidDesignator;
  3508. IF left # NIL THEN
  3509. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3510. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3511. END;
  3512. scope := NIL;
  3513. IF left.type = NIL THEN
  3514. Error(selectorDesignator.position,"field on nil typed designator");
  3515. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3516. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3517. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3518. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3519. module := symbol(SyntaxTree.Import).module;
  3520. IF module # NIL THEN
  3521. scope := module.moduleScope
  3522. ELSE
  3523. Error(left.position,"module not loaded");
  3524. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3525. END;
  3526. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3527. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3528. ASSERT(scope # NIL)
  3529. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3530. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3531. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3532. IF type IS SyntaxTree.EnumerationType THEN
  3533. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3534. ELSE
  3535. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3536. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3537. END;
  3538. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3539. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3540. ELSE
  3541. Error(selectorDesignator.position,"field on non-record type designator");
  3542. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3543. END;
  3544. symbol := NIL;
  3545. IF scope # NIL THEN
  3546. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3547. IF symbol # NIL THEN
  3548. ResolveSymbol(symbol);
  3549. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3550. symbol.MarkUsed
  3551. ELSE
  3552. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3553. IF VerboseErrorMessage THEN
  3554. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3555. Printout.Info("scope", scope);
  3556. Printout.Info("left", left);
  3557. Printout.Info("undeclared identifier",selectorDesignator);
  3558. Printout.Info("left resolved designator",left);
  3559. END
  3560. END;
  3561. END;
  3562. END;
  3563. resolvedExpression := result;
  3564. END VisitSelectorDesignator;
  3565. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3566. VAR len,idx: Basic.Integer;
  3567. BEGIN
  3568. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3569. IF idx < 0 THEN
  3570. Error(index.position,"index out of bounds (too small)")
  3571. ELSE
  3572. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3573. Error(index.position,"index out of bounds (too large)");
  3574. END;
  3575. END;
  3576. END;
  3577. END IndexCheck;
  3578. (*
  3579. - if index designator has not type, use newBaseType as its type
  3580. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3581. - special rule: if static array of dynamic array occurs, make it all dynamic
  3582. index designator type: new base type: new index designator type:
  3583. NIL z z
  3584. ARRAY [x, y] z ARRAY [x, y] OF z
  3585. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3586. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3587. *)
  3588. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3589. VAR
  3590. mathArrayType: SyntaxTree.MathArrayType;
  3591. makeDynamic: BOOLEAN;
  3592. BEGIN
  3593. IF indexDesignator.type = NIL THEN
  3594. indexDesignator.SetType(newBaseType)
  3595. ELSE
  3596. (* index designator must be a of math array type *)
  3597. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3598. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3599. (* determine if all arrays have to be made dynamic *)
  3600. makeDynamic :=
  3601. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3602. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3603. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3604. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3605. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3606. END;
  3607. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3608. mathArrayType.SetArrayBase(newBaseType)
  3609. END
  3610. END SetIndexBaseType;
  3611. (** check and append index list element to index designator of math array
  3612. - check validity of single index or array range
  3613. - compute new type
  3614. - if range then create new array type (calculate length of resulting array)
  3615. - otherwise take sourceArray.arrayBase as new type
  3616. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3617. **)
  3618. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3619. VAR
  3620. targetArray: SyntaxTree.MathArrayType;
  3621. first, last, step: SyntaxTree.Expression;
  3622. firstValue, lastValue, stepValue, length: Basic.Integer;
  3623. rangeExpression: SyntaxTree.RangeExpression;
  3624. BEGIN
  3625. IF indexListItem.type = SyntaxTree.invalidType THEN
  3626. (* error already handled *)
  3627. indexDesignator.parameters.AddExpression(indexListItem)
  3628. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3629. indexDesignator.HasRange;
  3630. indexDesignator.HasTensorRange;
  3631. indexDesignator.parameters.AddExpression(indexListItem);
  3632. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3633. ELSIF IsIntegerType(indexListItem.type.resolved) THEN
  3634. IndexCheck(indexListItem, sourceArray.length);
  3635. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3636. indexDesignator.parameters.AddExpression(indexListItem)
  3637. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3638. indexDesignator.HasRange;
  3639. (* if the range is given as an array range expression, check the validity of its components *)
  3640. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3641. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3642. first := rangeExpression.first;
  3643. last := rangeExpression.last;
  3644. step := rangeExpression.step;
  3645. (* perform static checks on range components *)
  3646. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3647. Error(indexListItem.position,"lower bound of array range too small")
  3648. END;
  3649. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3650. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3651. Error(indexListItem.position,"upper bound of array range too large")
  3652. END
  3653. END;
  3654. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3655. Error(indexListItem.position,"invalid step size")
  3656. END;
  3657. (* add conversions to size type *)
  3658. (* TODO: needed? *)
  3659. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.lenType, NIL));
  3660. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.lenType, NIL));
  3661. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.lenType, NIL));
  3662. END;
  3663. IF indexDesignator.hasTensorRange THEN
  3664. (* the index designator's base type is a tensor: leave it as is *)
  3665. ELSE
  3666. (* append a new math array to the index designator's base type *)
  3667. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3668. IF ~error THEN
  3669. (*
  3670. (* optimization: calculate length of target array for static ranges *)
  3671. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3672. IF IsStaticallyOpenRange(rangeExpression) THEN
  3673. (* range is open ('*'): reuse source array length as target array length *)
  3674. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3675. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3676. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3677. IF lastValue = MAX(LONGINT) THEN
  3678. IF IsIntegerValue(sourceArray.length, length) THEN
  3679. lastValue := length - 1;
  3680. isStaticTargetArrayLength := TRUE
  3681. ELSE
  3682. isStaticTargetArrayLength := FALSE
  3683. END
  3684. ELSE
  3685. isStaticTargetArrayLength := TRUE
  3686. END;
  3687. IF isStaticTargetArrayLength THEN
  3688. (* calculate static target array length *)
  3689. IF firstValue > lastValue THEN
  3690. length := 0
  3691. ELSE
  3692. length := 1 + lastValue - firstValue;
  3693. IF length MOD stepValue = 0 THEN
  3694. length := length DIV stepValue
  3695. ELSE
  3696. length := length DIV stepValue + 1
  3697. END
  3698. END;
  3699. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3700. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3701. ASSERT(targetArray.form = SyntaxTree.Static)
  3702. END
  3703. END
  3704. END
  3705. *)
  3706. END;
  3707. SetIndexBaseType(indexDesignator, targetArray)
  3708. END;
  3709. indexDesignator.parameters.AddExpression(indexListItem)
  3710. ELSE
  3711. Error(position,"invalid index list item");
  3712. END;
  3713. END AppendMathIndex;
  3714. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3715. VAR parameters: SyntaxTree.ExpressionList;
  3716. BEGIN
  3717. parameters := index.parameters;
  3718. IF (expression.type = NIL) THEN
  3719. Error(position, "invalid index");
  3720. ELSIF IsIntegerType(expression.type.resolved) THEN
  3721. IF over IS SyntaxTree.ArrayType THEN
  3722. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3723. ELSIF over IS SyntaxTree.StringType THEN
  3724. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3725. END;
  3726. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3727. parameters.AddExpression(expression);
  3728. ELSE
  3729. Error(position, "invalid index");
  3730. END;
  3731. END AppendIndex;
  3732. (** convert an expression to math array type
  3733. if expression is of math array type: return expression itself
  3734. if expression is of array-structured object type: return an index operator call on it
  3735. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3736. otherwise: return invalid expression
  3737. **)
  3738. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3739. VAR
  3740. result: SyntaxTree.Expression;
  3741. mathArrayType: SyntaxTree.MathArrayType;
  3742. BEGIN
  3743. IF expression.type = NIL THEN
  3744. result := SyntaxTree.invalidExpression
  3745. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3746. (* expression of math array type *)
  3747. result := expression
  3748. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3749. (* expression of array-structured object type *)
  3750. mathArrayType := MathArrayStructureOfType(expression.type);
  3751. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3752. ELSE
  3753. result := SyntaxTree.invalidExpression
  3754. END;
  3755. RETURN result
  3756. END ConvertToMathArray;
  3757. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3758. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3759. VAR
  3760. result: SyntaxTree.ExpressionList;
  3761. i: LONGINT;
  3762. BEGIN
  3763. result := SyntaxTree.NewExpressionList();
  3764. FOR i := 1 TO itemCount DO
  3765. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3766. END;
  3767. RETURN result
  3768. END ListOfOpenRanges;
  3769. (** create a procedure call designator for an index operator call on an array-structured object type
  3770. - use given index list as actual parameters
  3771. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3772. **)
  3773. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3774. VAR
  3775. operator: SyntaxTree.Operator;
  3776. expression: SyntaxTree.Expression;
  3777. actualParameters, tempList: SyntaxTree.ExpressionList;
  3778. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3779. result, tempDesignator: SyntaxTree.Designator;
  3780. recordType: SyntaxTree.RecordType;
  3781. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3782. i, hashValue, indexListSize, indexListKind: LONGINT;
  3783. castReturnType: SyntaxTree.MathArrayType;
  3784. BEGIN
  3785. ASSERT(IsArrayStructuredObjectType(left.type));
  3786. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3787. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3788. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3789. indexListSize := indexList.Length();
  3790. indexListKind := 0;
  3791. containsNonRange := FALSE;
  3792. FOR i := 0 TO indexList.Length() - 1 DO
  3793. indexListKind := indexListKind * 2;
  3794. expression := indexList.GetExpression(i);
  3795. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3796. INC(indexListKind)
  3797. ELSE
  3798. containsNonRange := TRUE
  3799. END
  3800. END;
  3801. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3802. (* select applicable index operator
  3803. - try to look up optimal index operator
  3804. - if not present, use operator on ranges
  3805. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3806. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3807. *)
  3808. usesGeneralOperator := FALSE;
  3809. IF rhs # NIL THEN
  3810. (* write operator *)
  3811. IF hashValue = -1 THEN
  3812. operator := NIL
  3813. ELSE
  3814. operator := recordType.arrayAccessOperators.write[hashValue];
  3815. END;
  3816. IF operator = NIL THEN
  3817. usesPureRangeOperator := TRUE;
  3818. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3819. operator := recordType.arrayAccessOperators.generalWrite;
  3820. usesGeneralOperator := TRUE
  3821. ELSE
  3822. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3823. operator := recordType.arrayAccessOperators.write[hashValue];
  3824. END
  3825. END
  3826. ELSE
  3827. (* read operator *)
  3828. IF hashValue = -1 THEN
  3829. operator := NIL
  3830. ELSE
  3831. operator := recordType.arrayAccessOperators.read[hashValue];
  3832. END;
  3833. IF operator = NIL THEN
  3834. usesPureRangeOperator := TRUE;
  3835. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3836. operator := recordType.arrayAccessOperators.generalRead;
  3837. usesGeneralOperator := TRUE
  3838. ELSE
  3839. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3840. operator := recordType.arrayAccessOperators.read[hashValue];
  3841. END
  3842. END
  3843. END;
  3844. IF operator = NIL THEN
  3845. Error(position, "call of undeclared [] operator");
  3846. result := SyntaxTree.invalidDesignator;
  3847. ELSE
  3848. (* determine if reshaping is needed *)
  3849. needsReshaping := containsNonRange & usesPureRangeOperator;
  3850. (* import OCArrayBase if reshaping is needed *)
  3851. IF needsReshaping & ~arrayBaseImported THEN
  3852. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3853. arrayBaseImported := TRUE
  3854. END;
  3855. (* add the index list item to the list of actual parameters
  3856. - for general operators: add a single inline array containing the index list items as parameter
  3857. - otherwise: add all index list items as individual parameters
  3858. *)
  3859. actualParameters := SyntaxTree.NewExpressionList();
  3860. IF usesGeneralOperator THEN
  3861. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3862. END;
  3863. FOR i := 0 TO indexListSize - 1 DO
  3864. expression := indexList.GetExpression(i);
  3865. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3866. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3867. tempList := SyntaxTree.NewExpressionList();
  3868. tempList.AddExpression(expression);
  3869. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3870. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3871. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3872. END;
  3873. IF usesGeneralOperator THEN
  3874. tempMathArrayExpression.elements.AddExpression(expression);
  3875. ELSE
  3876. actualParameters.AddExpression(expression)
  3877. END
  3878. END;
  3879. IF usesGeneralOperator THEN
  3880. actualParameters.AddExpression(tempMathArrayExpression)
  3881. END;
  3882. IF rhs # NIL THEN
  3883. (* add actual parameter for RHS *)
  3884. IF needsReshaping THEN
  3885. (* reshape using OCArrayBase.ExpandDimensions *)
  3886. tempList := SyntaxTree.NewExpressionList();
  3887. (* source array *)
  3888. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3889. tempList.AddExpression(rhs);
  3890. ELSE
  3891. (* convert scalar to one-dimensional array *)
  3892. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3893. tempMathArrayExpression.elements.AddExpression(rhs);
  3894. tempList.AddExpression(tempMathArrayExpression)
  3895. END;
  3896. (* list of kept dimensions *)
  3897. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3898. FOR i := 0 TO indexListSize - 1 DO
  3899. expression := indexList.GetExpression(i);
  3900. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3901. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3902. ELSE
  3903. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3904. END
  3905. END;
  3906. tempList.AddExpression(tempMathArrayExpression);
  3907. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3908. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3909. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3910. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3911. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3912. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3913. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3914. expression.SetType(castReturnType);
  3915. ELSE
  3916. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3917. END;
  3918. actualParameters.AddExpression(expression)
  3919. ELSE
  3920. actualParameters.AddExpression(rhs)
  3921. END
  3922. END;
  3923. (* add dereference operator and create procedure call designator *)
  3924. ASSERT(left IS SyntaxTree.Designator);
  3925. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3926. ASSERT(expression IS SyntaxTree.Designator);
  3927. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3928. IF (rhs = NIL) & needsReshaping THEN
  3929. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3930. tempList := SyntaxTree.NewExpressionList();
  3931. FOR i := 0 TO indexList.Length() - 1 DO
  3932. expression := indexList.GetExpression(i);
  3933. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3934. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3935. ELSE
  3936. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3937. END
  3938. END;
  3939. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3940. END;
  3941. IF rhs = NIL THEN
  3942. (* special rule: index read operator calls are considered to be assignable *)
  3943. result.SetAssignable(TRUE)
  3944. END;
  3945. (* put information about this index operator call into the resulting designator *)
  3946. result.SetRelatedAsot(left);
  3947. result.SetRelatedIndexList(indexList)
  3948. END;
  3949. RETURN result
  3950. END NewIndexOperatorCall;
  3951. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3952. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3953. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3954. pointer: BOOLEAN; designator: SyntaxTree.Designator;
  3955. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3956. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3957. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3958. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3959. CONST trace = FALSE;
  3960. BEGIN
  3961. IF trace THEN
  3962. FOR i := 0 TO actualParameters.Length()-1 DO
  3963. Printout.Info("par", actualParameters.GetExpression(i));
  3964. END;
  3965. END;
  3966. operator := scope.firstOperator;
  3967. WHILE(operator # NIL) DO
  3968. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3969. procedureType := operator.type(SyntaxTree.ProcedureType);
  3970. distance := Distance(system, procedureType,actualParameters);
  3971. IF trace THEN Printout.Info("check op ",operator) END;
  3972. IF distance < bestDistance THEN
  3973. IF trace THEN Printout.Info("taken op",operator) END;
  3974. bestDistance := distance;
  3975. bestOperator := operator;
  3976. END;
  3977. END;
  3978. operator := operator.nextOperator;
  3979. END;
  3980. END FindInScope;
  3981. BEGIN
  3982. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3983. IF oper = 0 THEN (* index *)
  3984. identifier := SyntaxTree.NewIdentifier("[]");
  3985. ELSE
  3986. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3987. END;
  3988. WHILE (recordType # NIL) DO
  3989. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3990. recordType := recordType.GetBaseRecord();
  3991. END;
  3992. RETURN bestOperator
  3993. END FindOperator;
  3994. BEGIN
  3995. type := left.type.resolved;
  3996. IF type IS SyntaxTree.RecordType THEN
  3997. pointer := FALSE;
  3998. recordType := type(SyntaxTree.RecordType);
  3999. ELSE
  4000. pointer := TRUE;
  4001. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  4002. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4003. END;
  4004. actualParameters := SyntaxTree.NewExpressionList();
  4005. IF parameters # NIL THEN
  4006. FOR i := 0 TO parameters.Length()-1 DO
  4007. expression := ResolveExpression(parameters.GetExpression(i));
  4008. actualParameters.AddExpression(expression);
  4009. END;
  4010. END;
  4011. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  4012. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4013. IF op # NIL THEN
  4014. designator := left(SyntaxTree.Designator);
  4015. IF pointer THEN designator := NewDereferenceDesignator(Basic.invalidPosition, designator) END;
  4016. expression := NewSymbolDesignator(position, designator , op);
  4017. ASSERT(expression IS SyntaxTree.Designator);
  4018. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  4019. result.SetRelatedAsot(left);
  4020. result.SetRelatedIndexList(parameters);
  4021. (* check if write operator exists, for var parameters *)
  4022. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  4023. actualParameters := SyntaxTree.NewExpressionList();
  4024. FOR i := 0 TO parameters.Length()-1 DO
  4025. expression := ResolveExpression(parameters.GetExpression(i));
  4026. actualParameters.AddExpression(expression);
  4027. END;
  4028. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  4029. actualParameters.AddExpression(rhs);
  4030. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  4031. IF op = NIL THEN rhs := NIL END;
  4032. END;
  4033. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  4034. ELSE
  4035. result := NIL;
  4036. END;
  4037. RETURN result;
  4038. END NewObjectOperatorCall;
  4039. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  4040. 1. convert bracket designator chains into a single one that contains separators
  4041. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  4042. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  4043. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  4044. - if an array or math array is indexed over, create index designator
  4045. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  4046. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  4047. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  4048. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  4049. - if an array-structured object type is indexed over, create procedure call designator
  4050. e.g.: a[x, y] -> a^."[]"(x, y)
  4051. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  4052. - a[i, *] = a[i][*]
  4053. - a[*, i] # a[*][i]
  4054. Because:
  4055. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  4056. - 'i-th column' = a[*, i]
  4057. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  4058. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  4059. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  4060. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  4061. **)
  4062. PROCEDURE VisitBracketDesignator*(bracketDesignator: SyntaxTree.BracketDesignator);
  4063. VAR
  4064. leftBracketDesignator: SyntaxTree.BracketDesignator;
  4065. indexDesignator: SyntaxTree.IndexDesignator;
  4066. designator: SyntaxTree.Designator;
  4067. type: SyntaxTree.Type;
  4068. recordType: SyntaxTree.RecordType;
  4069. expression, rhs: SyntaxTree.Expression;
  4070. indexList: SyntaxTree.ExpressionList;
  4071. i: LONGINT;
  4072. hasError, done: BOOLEAN;
  4073. PROCEDURE FinalizeIndexDesignator;
  4074. BEGIN
  4075. IF indexDesignator # NIL THEN
  4076. (* the end of a tensor has been reached: *)
  4077. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4078. SetIndexBaseType(indexDesignator, type);
  4079. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4080. designator := indexDesignator;
  4081. type := designator.type.resolved;
  4082. indexDesignator := NIL;
  4083. ASSERT(SyntaxTree.Resolved IN type.state)
  4084. END
  4085. END FinalizeIndexDesignator;
  4086. BEGIN
  4087. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4088. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4089. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4090. (* copy all index list entries including a separator to the left bracket designator *)
  4091. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4092. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4093. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4094. END;
  4095. (* propagate the related RHS *)
  4096. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4097. (* only resolve left bracket designator and use as final result *)
  4098. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4099. ELSE
  4100. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4101. designator := ResolveDesignator(bracketDesignator.left);
  4102. type := designator.type.resolved;
  4103. indexDesignator := NIL;
  4104. (*!!! clean up *)
  4105. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type)
  4106. OR (type IS SyntaxTree.RecordType)
  4107. THEN
  4108. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4109. IF resolvedExpression = NIL THEN
  4110. Error(bracketDesignator.position,"undefined operator");
  4111. resolvedExpression := SyntaxTree.invalidDesignator
  4112. END;
  4113. RETURN;
  4114. END;
  4115. i := 0;
  4116. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4117. expression := bracketDesignator.parameters.GetExpression(i);
  4118. expression := ResolveExpression(expression);
  4119. bracketDesignator.parameters.SetExpression(i, expression);
  4120. IF expression = SyntaxTree.indexListSeparator THEN
  4121. (* finalize an existing index designator if needed *)
  4122. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4123. INC(i)
  4124. ELSE
  4125. (* do auto-dereferencing if needed *)
  4126. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4127. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4128. & (i=0)*)
  4129. THEN
  4130. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4131. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4132. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4133. designator := SyntaxTree.invalidDesignator;
  4134. type := SyntaxTree.invalidType
  4135. ELSE
  4136. FinalizeIndexDesignator;
  4137. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4138. type := designator.type.resolved
  4139. END
  4140. END;
  4141. (* create a new index designator, if needed *)
  4142. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4143. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4144. indexDesignator.SetAssignable(designator.assignable);
  4145. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4146. (* designator := indexDesignator *)
  4147. END;
  4148. IF type = SyntaxTree.invalidType THEN
  4149. (* error already handled *)
  4150. INC(i)
  4151. ELSIF type IS SyntaxTree.ArrayType THEN
  4152. (* indexing over an array *)
  4153. ASSERT(indexDesignator # NIL);
  4154. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4155. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4156. INC(i)
  4157. ELSIF type IS SyntaxTree.StringType THEN
  4158. (* indexing over an array *)
  4159. ASSERT(indexDesignator # NIL);
  4160. AppendIndex(expression.position, indexDesignator, expression, type);
  4161. type := type(SyntaxTree.StringType).baseType.resolved;
  4162. INC(i)
  4163. ELSIF type IS SyntaxTree.MathArrayType THEN
  4164. (* indexing over a math array *)
  4165. ASSERT(indexDesignator # NIL);
  4166. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4167. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4168. INC(i)
  4169. ELSIF IsArrayStructuredObjectType(type) THEN
  4170. (* indexing over ASOTs *)
  4171. FinalizeIndexDesignator;
  4172. ASSERT(type IS SyntaxTree.PointerType);
  4173. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4174. (*
  4175. - collect index list items from bracket designator that belong to ASOT
  4176. - check for errors
  4177. *)
  4178. indexList := SyntaxTree.NewExpressionList();
  4179. hasError := FALSE;
  4180. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4181. (* indexing over tensor ASOT:
  4182. - stop at index list end or separator
  4183. - dimensionality is given by number of index list items
  4184. *)
  4185. done := FALSE;
  4186. WHILE ~done DO
  4187. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4188. done := TRUE;
  4189. ELSE
  4190. expression := bracketDesignator.parameters.GetExpression(i);
  4191. IF expression = SyntaxTree.indexListSeparator THEN
  4192. done := TRUE;
  4193. ELSE
  4194. expression := ResolveExpression(expression);
  4195. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4196. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4197. hasError := TRUE
  4198. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4199. Error(expression.position, "integer or range expected");
  4200. expression := SyntaxTree.invalidExpression;
  4201. hasError := TRUE
  4202. END;
  4203. indexList.AddExpression(expression)
  4204. END;
  4205. INC(i)
  4206. END
  4207. END
  4208. ELSE
  4209. (* indexing over non-tensor ASOT:
  4210. - ignore separators
  4211. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4212. *)
  4213. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4214. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4215. expression := bracketDesignator.parameters.GetExpression(i);
  4216. ELSE
  4217. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4218. END;
  4219. IF expression # SyntaxTree.indexListSeparator THEN
  4220. expression := ResolveExpression(expression);
  4221. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4222. Error(expression.position, "integer or range expected");
  4223. expression := SyntaxTree.invalidExpression;
  4224. hasError := TRUE
  4225. END;
  4226. indexList.AddExpression(expression)
  4227. END;
  4228. INC(i)
  4229. END;
  4230. END;
  4231. IF hasError THEN
  4232. designator := SyntaxTree.invalidDesignator;
  4233. type := SyntaxTree.invalidType;
  4234. ELSE
  4235. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4236. and the last entry in the index list belongs to the array-structured object type in question.
  4237. E.g.: for a 2-dimensional array-structured object type:
  4238. - 'lhs := asot[1, 2]' -> read mode
  4239. - 'asot[1, 2] := rhs' -> write mode
  4240. - 'asot[1, 2, 3] := rhs' -> read mode
  4241. *)
  4242. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4243. rhs := bracketDesignator.relatedRhs
  4244. ELSE
  4245. rhs := NIL
  4246. END;
  4247. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4248. type := designator.type
  4249. END
  4250. ELSE
  4251. Error(expression.position,"indexing over non-array type");
  4252. designator := SyntaxTree.invalidDesignator;
  4253. type := SyntaxTree.invalidType;
  4254. INC(i)
  4255. END
  4256. END
  4257. END;
  4258. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4259. resolvedExpression := designator
  4260. END
  4261. END VisitBracketDesignator;
  4262. (** check and resolve expression list
  4263. - resolve each expression in an expression list
  4264. - returns true if and only if all statements could have successfully been resolved
  4265. **)
  4266. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4267. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4268. BEGIN
  4269. result := TRUE;
  4270. FOR i := 0 TO expressionList.Length()-1 DO
  4271. expression := ResolveExpression(expressionList.GetExpression(i));
  4272. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4273. expressionList.SetExpression(i,expression);
  4274. END;
  4275. RETURN result
  4276. END ExpressionList;
  4277. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4278. BEGIN
  4279. type := type.resolved;
  4280. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4281. RETURN TRUE
  4282. ELSIF system.CanPassInRegister # NIL THEN
  4283. RETURN system.CanPassInRegister(type);
  4284. ELSE
  4285. RETURN FALSE
  4286. END;
  4287. END CanPassInRegister;
  4288. (** return procedure call designator left(actualParameters)
  4289. - check realtime procedure call in realtime procedure
  4290. - check number of parameters
  4291. - check parameter compatibility
  4292. return invalidDesignator if error
  4293. **)
  4294. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4295. VAR result: SyntaxTree.Designator;
  4296. numberFormalParameters, numberActualParameters: LONGINT;
  4297. formalType: SyntaxTree.ProcedureType;
  4298. formalParameter: SyntaxTree.Parameter;
  4299. actualParameter: SyntaxTree.Expression;
  4300. i: LONGINT;
  4301. self: SyntaxTree.Expression;
  4302. BEGIN
  4303. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4304. result := SyntaxTree.invalidDesignator;
  4305. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4306. numberFormalParameters := formalType.numberParameters;
  4307. numberActualParameters := actualParameters.Length();
  4308. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4309. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4310. END;
  4311. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4312. self := left.left;
  4313. IF (self # NIL) & ~IsVariable(self) THEN
  4314. Error(self.position, "Non-variable expression on variable receiver");
  4315. END;
  4316. END;
  4317. IF ~ExpressionList(actualParameters) THEN
  4318. result := SyntaxTree.invalidDesignator
  4319. ELSE
  4320. IF numberActualParameters <= numberFormalParameters THEN
  4321. formalParameter := formalType.firstParameter;
  4322. FOR i := 0 TO numberActualParameters-1 DO
  4323. actualParameter := actualParameters.GetExpression(i);
  4324. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4325. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4326. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4327. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4328. ELSE
  4329. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4330. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4331. END;
  4332. actualParameters.SetExpression(i,actualParameter);
  4333. END;
  4334. formalParameter := formalParameter.nextParameter;
  4335. END;
  4336. WHILE (formalParameter # NIL) DO
  4337. IF formalParameter.defaultValue # NIL THEN
  4338. actualParameters.AddExpression(formalParameter.defaultValue);
  4339. formalParameter := formalParameter.nextParameter
  4340. ELSE
  4341. Error(position, "less actual than formal parameters");
  4342. formalParameter := NIL;
  4343. END;
  4344. END;
  4345. ELSE
  4346. Error(position, "more actual than formal parameters")
  4347. END;
  4348. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4349. result.SetAssignable(FALSE);
  4350. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4351. END;
  4352. RETURN result
  4353. END NewProcedureCallDesignator;
  4354. (**
  4355. builtin call designator generated in VisitParameterDesignator
  4356. -> nothing to be resolved
  4357. **)
  4358. PROCEDURE VisitTypeGuardDesignator*(x: SyntaxTree.TypeGuardDesignator);
  4359. BEGIN
  4360. resolvedExpression := x;
  4361. END VisitTypeGuardDesignator;
  4362. (**
  4363. builtin call designator generated in VisitParameterDesignator
  4364. -> nothing to be resolved
  4365. **)
  4366. PROCEDURE VisitBuiltinCallDesignator*(x: SyntaxTree.BuiltinCallDesignator);
  4367. BEGIN
  4368. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4369. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4370. ASSERT(resolvedExpression.type # NIL);
  4371. ELSIF ExpressionList(x.parameters) THEN
  4372. resolvedExpression := x;
  4373. END;
  4374. END VisitBuiltinCallDesignator;
  4375. (**
  4376. procedure call designator generated in VisitParameterDesignator
  4377. -> nothing to be resolved
  4378. **)
  4379. PROCEDURE VisitProcedureCallDesignator*(x: SyntaxTree.ProcedureCallDesignator);
  4380. BEGIN
  4381. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4382. resolvedExpression := x;
  4383. END VisitProcedureCallDesignator;
  4384. (** return true if x is a variable else return false and report error **)
  4385. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4386. VAR result: BOOLEAN;
  4387. BEGIN
  4388. result := TRUE;
  4389. IF x = SyntaxTree.invalidExpression THEN
  4390. result := FALSE;
  4391. ELSIF ~IsVariable(x) THEN
  4392. Error(x.position,"non variable expression");
  4393. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4394. result := FALSE;
  4395. END;
  4396. RETURN result
  4397. END CheckVariable;
  4398. (**
  4399. if expression x is of basic type then return true else report error and return false
  4400. **)
  4401. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4402. VAR result: BOOLEAN;
  4403. BEGIN
  4404. result := FALSE;
  4405. IF x = SyntaxTree.invalidExpression THEN
  4406. ELSIF ~IsBasicType(x.type) THEN
  4407. Error(x.position,"is no basic type");
  4408. result := FALSE
  4409. ELSE result := TRUE
  4410. END;
  4411. RETURN result
  4412. END CheckBasicType;
  4413. (**
  4414. if expression x is of number type then return true else report error and return false
  4415. **)
  4416. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4417. VAR result: BOOLEAN;
  4418. BEGIN
  4419. result := FALSE;
  4420. IF x = SyntaxTree.invalidExpression THEN
  4421. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4422. Error(x.position,"is non number type");
  4423. ELSE result := TRUE
  4424. END;
  4425. RETURN result
  4426. END CheckNumberType;
  4427. (**
  4428. if expression x is of number or size type but not complex then return true else report error and return false
  4429. **)
  4430. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4431. VAR result: BOOLEAN;
  4432. BEGIN
  4433. result := FALSE;
  4434. IF x = SyntaxTree.invalidExpression THEN
  4435. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4436. Error(x.position,"is complex type");
  4437. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4438. Error(x.position,"is non number type");
  4439. ELSE result := TRUE
  4440. END;
  4441. RETURN result
  4442. END CheckNonComplexNumberSizeType;
  4443. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4444. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4445. BEGIN
  4446. result := FALSE; type := x.type.resolved;
  4447. IF x = SyntaxTree.invalidExpression THEN
  4448. 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
  4449. TRACE(type.sizeInBits);
  4450. TRACE(system.addressType.sizeInBits);
  4451. Error(x.position,"is no address type");
  4452. ELSE result := TRUE
  4453. END;
  4454. RETURN result
  4455. END CheckAddressType;
  4456. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4457. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4458. BEGIN
  4459. result := FALSE; type := x.type.resolved;
  4460. IF x = SyntaxTree.invalidExpression THEN
  4461. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4462. Error(x.position,"is no size type");
  4463. ELSE result := TRUE
  4464. END;
  4465. RETURN result
  4466. END CheckSizeType;
  4467. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4468. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4469. BEGIN
  4470. result := FALSE; type := x.type.resolved;
  4471. IF x = SyntaxTree.invalidExpression THEN
  4472. 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
  4473. Error(x.position,"is no object type");
  4474. ELSE result := TRUE
  4475. END;
  4476. RETURN result
  4477. END CheckObjectType;
  4478. (**
  4479. if expression x is of integer type then return true else report error and return false
  4480. **)
  4481. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4482. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4483. BEGIN
  4484. result := FALSE; type := x.type.resolved;
  4485. IF x = SyntaxTree.invalidExpression THEN
  4486. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4487. Error(x.position,"is no integer type");
  4488. ELSE result := TRUE
  4489. END;
  4490. RETURN result
  4491. END CheckIntegerType;
  4492. (**
  4493. if expression x is of character type then return true else report error and return false
  4494. **)
  4495. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4496. VAR result: BOOLEAN;
  4497. BEGIN
  4498. result := FALSE;
  4499. IF x = SyntaxTree.invalidExpression THEN
  4500. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4501. Error(x.position,"is no character type");
  4502. ELSE result := TRUE
  4503. END;
  4504. RETURN result
  4505. END CheckCharacterType;
  4506. (**
  4507. if expression x is of real type then return true else report error and return false
  4508. **)
  4509. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4510. VAR result: BOOLEAN;
  4511. BEGIN
  4512. result := FALSE;
  4513. IF x = SyntaxTree.invalidExpression THEN
  4514. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4515. Error(x.position,"is no float type");
  4516. ELSE result := TRUE
  4517. END;
  4518. RETURN result
  4519. END CheckRealType;
  4520. (**
  4521. if expression x is of range type then return true else report error and return false
  4522. **)
  4523. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4524. VAR result: BOOLEAN;
  4525. BEGIN
  4526. result := FALSE;
  4527. IF x = SyntaxTree.invalidExpression THEN
  4528. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4529. Error(x.position,"is no range type");
  4530. ELSE result := TRUE
  4531. END;
  4532. RETURN result
  4533. END CheckRangeType;
  4534. (**
  4535. if expression x is of boolean type then return true else report error and return false
  4536. **)
  4537. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4538. VAR result: BOOLEAN;
  4539. BEGIN
  4540. result := FALSE;
  4541. IF x = SyntaxTree.invalidExpression THEN
  4542. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4543. Error(x.position,"is no boolean type");
  4544. ELSE result := TRUE
  4545. END;
  4546. RETURN result
  4547. END CheckBooleanType;
  4548. (**
  4549. if expression x is of set type then return true else report error and return false
  4550. **)
  4551. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4552. VAR result: BOOLEAN;
  4553. BEGIN
  4554. result := FALSE;
  4555. IF x = SyntaxTree.invalidExpression THEN
  4556. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4557. Error(x.position,"is no set type");
  4558. ELSE result := TRUE
  4559. END;
  4560. RETURN result
  4561. END CheckSetType;
  4562. (**
  4563. if expression x is of string or array of character type then return true else report error and return false
  4564. **)
  4565. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4566. VAR result: BOOLEAN;
  4567. BEGIN
  4568. result := FALSE;
  4569. IF x = SyntaxTree.invalidExpression THEN
  4570. ELSIF ~IsStringType(x.type.resolved) THEN
  4571. Error(x.position,"is no string type");
  4572. ELSE result := TRUE
  4573. END;
  4574. RETURN result
  4575. END CheckStringType;
  4576. (**
  4577. if expression x is a type declaration type return true else report error and return false
  4578. **)
  4579. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4580. VAR result: BOOLEAN;
  4581. BEGIN
  4582. result := FALSE;
  4583. IF x = SyntaxTree.invalidExpression THEN
  4584. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4585. Error(x.position,"is not a type declaration");
  4586. ELSE result := TRUE
  4587. END;
  4588. RETURN result
  4589. END CheckTypeDeclarationType;
  4590. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4591. VAR result: BOOLEAN;
  4592. BEGIN
  4593. result := FALSE;
  4594. IF x = SyntaxTree.invalidExpression THEN
  4595. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4596. result := TRUE;
  4597. value := x.resolved(SyntaxTree.IntegerValue).value;
  4598. ELSE
  4599. Error(x.position,"expression is not an integer constant");
  4600. END;
  4601. RETURN result;
  4602. END CheckIntegerValue;
  4603. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4604. VAR result: BOOLEAN;
  4605. BEGIN
  4606. result := FALSE;
  4607. IF x = SyntaxTree.invalidExpression THEN
  4608. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4609. result := TRUE;
  4610. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4611. ELSE
  4612. Error(x.position,"expression is not an integer constant");
  4613. END;
  4614. RETURN result;
  4615. END CheckStringValue;
  4616. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4617. BEGIN
  4618. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4619. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, maxSizeInBits)
  4620. ELSE
  4621. RETURN FALSE
  4622. END;
  4623. END IsUnsignedValue;
  4624. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4625. BEGIN
  4626. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4627. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).value, system.addressType.sizeInBits)
  4628. ELSE
  4629. RETURN FALSE
  4630. END
  4631. END IsAddressValue;
  4632. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4633. BEGIN
  4634. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4635. END IsAddressExpression;
  4636. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4637. BEGIN
  4638. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4639. END IsSizeExpression;
  4640. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  4641. VAR result: BOOLEAN;
  4642. BEGIN
  4643. result := FALSE;
  4644. IF x = SyntaxTree.invalidExpression THEN
  4645. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4646. result := TRUE;
  4647. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4648. ELSE
  4649. Error(x.position,"expression is not an integer constant");
  4650. END;
  4651. RETURN result;
  4652. END CheckEnumerationValue;
  4653. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4654. VAR result: BOOLEAN;
  4655. BEGIN
  4656. result := FALSE;
  4657. IF x = SyntaxTree.invalidExpression THEN
  4658. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4659. result := TRUE;
  4660. value := x.resolved(SyntaxTree.CharacterValue).value;
  4661. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4662. result := TRUE;
  4663. value := x.resolved(SyntaxTree.StringValue).value[0];
  4664. ELSE
  4665. Error(x.position,"expression is not a character constant");
  4666. END;
  4667. RETURN result;
  4668. END CheckCharacterValue;
  4669. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer; includeZero: BOOLEAN): BOOLEAN;
  4670. VAR result: BOOLEAN;
  4671. BEGIN
  4672. result := FALSE;
  4673. IF x = SyntaxTree.invalidExpression THEN
  4674. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4675. value := x.resolved(SyntaxTree.IntegerValue).value;
  4676. IF (value > 0) OR includeZero & (value = 0) THEN
  4677. result := TRUE;
  4678. ELSE
  4679. Error(x.position,"integer is not positive");
  4680. END
  4681. ELSE
  4682. Error(x.position,"expression is not an integer constant");
  4683. END;
  4684. RETURN result;
  4685. END CheckPositiveIntegerValue;
  4686. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4687. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4688. BEGIN
  4689. result := FALSE;
  4690. IF x = SyntaxTree.invalidExpression THEN
  4691. ELSE
  4692. type := x.type.resolved;
  4693. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4694. portType := type(SyntaxTree.PortType);
  4695. result := TRUE
  4696. ELSE
  4697. Error(x.position,"no port type");
  4698. END;
  4699. END;
  4700. RETURN result
  4701. END CheckPortType;
  4702. (* move to builtin procedure call statement ?
  4703. remove builtin procedure call designator ?
  4704. *)
  4705. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4706. VAR
  4707. numberActualParameters,numberFormalParameters: LONGINT;
  4708. formalParameter: SyntaxTree.Parameter;
  4709. actualParameter: SyntaxTree.Expression;
  4710. procedureType: SyntaxTree.ProcedureType;
  4711. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4712. inPort, outPort: SyntaxTree.PortType;
  4713. constructor: SyntaxTree.Procedure;
  4714. type0,type1,type2: SyntaxTree.Type;
  4715. type,base,parameterType: SyntaxTree.Type;
  4716. arrayType: SyntaxTree.ArrayType;
  4717. i: LONGINT; i0, i1, value: Basic.Integer;
  4718. r,r0,r1,im: LONGREAL;
  4719. c: CHAR;
  4720. id: LONGINT;
  4721. b: BOOLEAN;
  4722. first: LONGINT;
  4723. mathArrayType: SyntaxTree.MathArrayType;
  4724. customBuiltin: SyntaxTree.CustomBuiltin;
  4725. PROCEDURE CheckArity(from,to: Basic.Integer): BOOLEAN;
  4726. VAR resultB: BOOLEAN;
  4727. BEGIN
  4728. IF numberActualParameters < from THEN
  4729. Error(position, "less actual than formal parameters");
  4730. result := SyntaxTree.invalidExpression;
  4731. resultB := FALSE;
  4732. ELSIF numberActualParameters > to THEN
  4733. Error(position, "more actual than formal parameters");
  4734. result := SyntaxTree.invalidExpression;
  4735. resultB := FALSE;
  4736. ELSE
  4737. resultB := TRUE;
  4738. END;
  4739. RETURN resultB
  4740. END CheckArity;
  4741. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4742. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4743. BEGIN
  4744. WHILE modifier # NIL DO
  4745. symbol := cellType.FindProperty(modifier.identifier);
  4746. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4747. propertyType := symbol.type.resolved;
  4748. modifierType := modifier.expression.type.resolved;
  4749. IF ~CompatibleTo(system, modifierType, propertyType) &
  4750. ~(
  4751. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4752. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4753. Error(modifier.position,"incompatible to cell property");
  4754. END;
  4755. ELSE
  4756. Error(modifier.position, "undefined property");
  4757. END;
  4758. modifier := modifier.nextModifier;
  4759. END;
  4760. END CheckModifiers;
  4761. BEGIN
  4762. type := NIL; result := NIL;
  4763. type0 := NIL; type1 := NIL; type2 := NIL;
  4764. numberActualParameters := actualParameters.Length();
  4765. IF numberActualParameters>0 THEN
  4766. parameter0 := actualParameters.GetExpression(0);
  4767. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4768. Error(parameter0.position,"forbidden type-less argument");
  4769. result := SyntaxTree.invalidExpression
  4770. END
  4771. END;
  4772. IF numberActualParameters >1 THEN
  4773. parameter1 := actualParameters.GetExpression(1);
  4774. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4775. ELSE
  4776. Error(parameter1.position,"forbidden type-less argument");
  4777. result := SyntaxTree.invalidExpression
  4778. END
  4779. END;
  4780. IF numberActualParameters >2 THEN
  4781. parameter2 := actualParameters.GetExpression(2);
  4782. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4783. ELSE
  4784. Error(parameter2.position,"forbidden type-less argument");
  4785. result := SyntaxTree.invalidExpression
  4786. END
  4787. END;
  4788. IF returnType # NIL THEN
  4789. id := Global.New;
  4790. result := NIL;
  4791. ELSE
  4792. id := builtin.id;
  4793. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4794. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4795. END;
  4796. END;
  4797. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4798. ELSIF result # NIL THEN type := result.type (* operator *)
  4799. ELSE
  4800. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4801. result(SyntaxTree.Designator).SetLeft(left);
  4802. IF returnType # NIL THEN
  4803. type := returnType;
  4804. END;
  4805. (* ---- ASSERT ----- *)
  4806. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4807. IF CheckBooleanType(parameter0) THEN
  4808. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4809. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4810. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4811. *)
  4812. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4813. Error(position, "static assertion failed");
  4814. END;
  4815. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4816. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4817. rules imposed by the architecture / current runtime
  4818. *)
  4819. END;
  4820. END;
  4821. (* ---- COPY ----- *)
  4822. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4823. IF~IsStringType(type0) THEN
  4824. Error(parameter0.position,"no string type");
  4825. END;
  4826. IF ~IsStringType(type1) THEN
  4827. Error(parameter1.position,"no string type");
  4828. ELSIF CheckVariable(parameter1) THEN
  4829. IF (type0 IS SyntaxTree.StringType) THEN
  4830. arrayType := type1(SyntaxTree.ArrayType);
  4831. IF arrayType.form = SyntaxTree.Static THEN
  4832. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4833. Error(position, "destination length smaller than source length")
  4834. END;
  4835. END;
  4836. END;
  4837. END;
  4838. (* ---- INC, DEC----- *)
  4839. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4840. IF numberActualParameters = 1 THEN
  4841. parameter1 :=Global.NewIntegerValue(system,position,1);
  4842. actualParameters.AddExpression(parameter1);
  4843. END;
  4844. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4845. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4846. Error(position, "incompatible increment");
  4847. ELSE
  4848. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4849. actualParameters.SetExpression(1,parameter1);
  4850. END;
  4851. END;
  4852. (* ---- EXCL, INCL----- *)
  4853. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4854. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4855. IF IsIntegerValue(parameter1,i0) THEN
  4856. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4857. Error(position, "parameter out of SET range")
  4858. END;
  4859. END;
  4860. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4861. actualParameters.SetExpression(1,parameter1);
  4862. END;
  4863. (* ---- HALT, SYSTEM.HALT ----- *)
  4864. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4865. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4866. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4867. rules imposed by the architecture / current runtime
  4868. *)
  4869. END;
  4870. (* ---- WAIT ----- *)
  4871. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4872. IF CheckObjectType(parameter0) THEN
  4873. END;
  4874. (* ---- NEW ----- *)
  4875. ELSIF (id = Global.New) THEN
  4876. IF returnType # NIL THEN
  4877. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4878. ELSE
  4879. first := 1;
  4880. END;
  4881. IF CheckArity(first,Infinity) THEN
  4882. IF currentIsRealtime THEN
  4883. Error(position, "forbidden new in realtime block");
  4884. END;
  4885. IF IsUnsafePointer(type0) THEN
  4886. Error(position, "forbidden new on unsafe pointer");
  4887. END;
  4888. (* check constructor *)
  4889. IF (first =0) OR CheckVariable(parameter0) THEN
  4890. IF type0 IS SyntaxTree.PointerType THEN
  4891. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4892. ELSIF type0 IS SyntaxTree.CellType THEN
  4893. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4894. ELSE
  4895. Error(position, "forbidden new on value type");
  4896. END;
  4897. IF type0 IS SyntaxTree.ArrayType THEN
  4898. arrayType := type0(SyntaxTree.ArrayType);
  4899. IF arrayType.form = SyntaxTree.Static THEN
  4900. i := first
  4901. ELSIF arrayType.form = SyntaxTree.Open THEN
  4902. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4903. ELSE HALT(100)
  4904. END;
  4905. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4906. i := first;
  4907. REPEAT
  4908. actualParameter := actualParameters.GetExpression(i);
  4909. IF CheckSizeType(actualParameter) THEN
  4910. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.lenType,NIL);
  4911. actualParameters.SetExpression(i,actualParameter);
  4912. END;
  4913. INC(i);
  4914. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4915. END;
  4916. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4917. IF type0(SyntaxTree.RecordType).isAbstract THEN
  4918. Error(position, "forbidden new on abstract object");
  4919. END;
  4920. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4921. IF constructor = NIL THEN
  4922. IF CheckArity(first,first) THEN END;
  4923. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4924. Error(position, "new on object with hidden constructor");
  4925. ELSE
  4926. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4927. numberFormalParameters := procedureType.numberParameters;
  4928. IF numberActualParameters-first <= numberFormalParameters THEN
  4929. formalParameter := procedureType.firstParameter;
  4930. FOR i := first TO numberActualParameters-1 DO
  4931. actualParameter := actualParameters.GetExpression(i);
  4932. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4933. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4934. ELSE
  4935. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4936. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4937. END;
  4938. actualParameters.SetExpression(i,actualParameter);
  4939. END;
  4940. formalParameter := formalParameter.nextParameter;
  4941. END;
  4942. WHILE (formalParameter # NIL) DO
  4943. IF formalParameter.defaultValue # NIL THEN
  4944. actualParameters.AddExpression(formalParameter.defaultValue);
  4945. formalParameter := formalParameter.nextParameter
  4946. ELSE
  4947. Error(position, "less actual than formal parameters");
  4948. formalParameter := NIL;
  4949. END;
  4950. END;
  4951. ELSE
  4952. Error(position, "more actual than formal parameters")
  4953. END;
  4954. END;
  4955. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4956. mathArrayType := type0(SyntaxTree.MathArrayType);
  4957. IF mathArrayType.form = SyntaxTree.Static THEN
  4958. Error(position, "new on static array");
  4959. ELSE
  4960. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4961. i0 := first+1; i1 := Infinity;
  4962. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4963. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4964. i1 := i0;
  4965. ELSE HALT(100);
  4966. END;
  4967. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4968. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4969. base := ArrayBase(type0,MAX(LONGINT));
  4970. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4971. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4972. IF ~CompatibleTo(system,type0,parameterType) THEN
  4973. Error(parameter0.position,"incompatible parameter in new");
  4974. result := SyntaxTree.invalidExpression;
  4975. ELSE
  4976. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4977. END;
  4978. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4979. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  4980. IF ~CompatibleTo(system,type1,parameterType) THEN
  4981. Error(parameter1.position,"parameter incompatible to math array len type");
  4982. result := SyntaxTree.invalidExpression;
  4983. ELSE
  4984. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4985. END;
  4986. ELSE
  4987. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4988. i := first;
  4989. REPEAT
  4990. actualParameter := actualParameters.GetExpression(i);
  4991. IF CheckSizeType(actualParameter) THEN
  4992. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4993. actualParameters.SetExpression(i,actualParameter);
  4994. END;
  4995. INC(i);
  4996. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4997. END;
  4998. END;
  4999. END;
  5000. ELSIF type0 IS SyntaxTree.CellType THEN
  5001. IF ~(currentIsCellNet) THEN
  5002. Error(position, "cell allocation outside activeCells ");
  5003. ELSE
  5004. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  5005. IF (constructor = NIL) & CheckArity(1,1) THEN
  5006. (* ok *)
  5007. ELSE
  5008. procedureType := constructor.type(SyntaxTree.ProcedureType);
  5009. numberFormalParameters := procedureType.numberParameters;
  5010. DEC(numberActualParameters);
  5011. IF numberActualParameters <= numberFormalParameters THEN
  5012. formalParameter := procedureType.firstParameter;
  5013. FOR i := first TO numberActualParameters DO
  5014. actualParameter := actualParameters.GetExpression(i);
  5015. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  5016. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5017. ELSE
  5018. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  5019. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  5020. END;
  5021. actualParameters.SetExpression(i,actualParameter);
  5022. END;
  5023. formalParameter := formalParameter.nextParameter;
  5024. END;
  5025. WHILE (formalParameter # NIL) DO
  5026. IF formalParameter.defaultValue # NIL THEN
  5027. actualParameters.AddExpression(formalParameter.defaultValue);
  5028. formalParameter := formalParameter.nextParameter
  5029. ELSE
  5030. Error(position, "less actual than formal parameters");
  5031. formalParameter := NIL;
  5032. END;
  5033. END;
  5034. ELSE
  5035. Error(position, "more actual than formal parameters")
  5036. END;
  5037. END;
  5038. END;
  5039. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  5040. activeCellsStatement := TRUE;
  5041. ELSE
  5042. Error(position, "cannot be allocated");
  5043. END;
  5044. END;
  5045. END;
  5046. (* ---- DISPOSE ----- *)
  5047. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  5048. IF ~IsPointerType(parameter0.type) THEN
  5049. Error(parameter0.position,"is not a pointer")
  5050. ELSIF ~IsDisposable(parameter0.type) THEN
  5051. Error(parameter0.position,"is not disposable")
  5052. ELSIF CheckVariable(parameter0) THEN (* ok *)
  5053. END
  5054. (* ---- GETPROCEDURE ----- *)
  5055. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  5056. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  5057. IF CheckVariable(parameter2) THEN
  5058. IF ~GetProcedureAllowed(parameter2.type) THEN
  5059. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  5060. END;
  5061. END;
  5062. END;
  5063. (* ---- ABS ----- *)
  5064. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  5065. (* note: ABS on complex numbers is done using overloading *)
  5066. IF CheckNonComplexNumberSizeType(parameter0) THEN
  5067. type := type0;
  5068. IF IsIntegerValue(parameter0,i0) THEN
  5069. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5070. type := Global.GetIntegerType(system,ABS(i0));
  5071. ELSIF IsRealValue(parameter0,r) THEN
  5072. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  5073. END;
  5074. ELSE
  5075. type := SyntaxTree.invalidType;
  5076. END;
  5077. (* ---- ASH, ASR ----- *)
  5078. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5079. type := type0;
  5080. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5081. (*
  5082. ConvertOperands(parameter0,parameter1); (* same type *)
  5083. *)
  5084. type := parameter0.type;
  5085. IF IsIntegerValue(parameter0,i0) THEN
  5086. IF IsIntegerValue(parameter1,i1) THEN
  5087. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5088. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5089. result := ResolveExpression(result);
  5090. type := Global.GetIntegerType(system,i0);
  5091. END;
  5092. END;
  5093. IF type.resolved.sizeInBits < 32 THEN
  5094. type := system.longintType;
  5095. END;
  5096. (*!compatibility with release, remove when resolved
  5097. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5098. *)
  5099. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5100. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5101. actualParameters.SetExpression(0,parameter0);
  5102. actualParameters.SetExpression(1,parameter1);
  5103. END;
  5104. (* ---- CAP ----- *)
  5105. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5106. type := system.characterType;
  5107. IF CheckCharacterType (parameter0) THEN
  5108. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5109. actualParameters.SetExpression(0,parameter0);
  5110. IF IsCharacterValue(parameter0,c) THEN
  5111. IF (c <= "z") & (c >= "a") THEN
  5112. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5113. ELSE
  5114. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5115. END;
  5116. END;
  5117. END;
  5118. (* ---- CHR ----- *)
  5119. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5120. IF id = Global.Chr THEN
  5121. type := system.characterType
  5122. ELSE
  5123. type := system.characterType32
  5124. END;
  5125. IF CheckIntegerType(parameter0) THEN
  5126. IF IsIntegerValue(parameter0,i0) THEN
  5127. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5128. result := ResolveExpression(result);
  5129. ELSE
  5130. (*
  5131. result := NewConversion(parameter0.position,parameter0,type);
  5132. *)
  5133. END;
  5134. END
  5135. (* ---- ENTIER ----- *)
  5136. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5137. type := system.longintType;
  5138. IF CheckRealType(parameter0) THEN
  5139. IF IsRealValue(parameter0,r) THEN
  5140. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5141. type := Global.GetIntegerType(system,ENTIER(r));
  5142. END
  5143. END;
  5144. (* ---- ENTIERH ----- *)
  5145. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5146. type := system.hugeintType;
  5147. IF CheckRealType(parameter0) THEN
  5148. IF IsRealValue(parameter0,r) THEN
  5149. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5150. END
  5151. END;
  5152. (* ---- LEN ----- *)
  5153. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5154. type := system.lenType;
  5155. base := type0;
  5156. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5157. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5158. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5159. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5160. Error(position, "forbidden len on unsafe pointer");
  5161. END;
  5162. type0 := base;
  5163. ELSE
  5164. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5165. type0 := parameter0.type.resolved;
  5166. actualParameters.SetExpression(0,parameter0);
  5167. base := type0;
  5168. END;
  5169. END;
  5170. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5171. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5172. IF i1 < 0 THEN
  5173. Error(position, "invalid dimension");
  5174. base := SyntaxTree.invalidType;
  5175. ELSE
  5176. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5177. IF (base # NIL) & Indexable(base) THEN
  5178. ELSE
  5179. Error(position, "len on no array");
  5180. IF VerboseErrorMessage THEN
  5181. Printout.Info("base",base);
  5182. END;
  5183. base := SyntaxTree.invalidType;
  5184. END;
  5185. END;
  5186. IF numberActualParameters=2 THEN
  5187. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5188. actualParameters.SetExpression(1,parameter1);
  5189. ELSIF base IS SyntaxTree.MathArrayType THEN
  5190. Error(position, "missing dimension specification");
  5191. END;
  5192. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5193. IF base IS SyntaxTree.ArrayType THEN
  5194. arrayType := base(SyntaxTree.ArrayType);
  5195. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,value) THEN
  5196. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5197. result := Global.NewIntegerValue(system,position,value);
  5198. type := result.type;(* arrayType.length.type;*)
  5199. ASSERT(type # NIL);
  5200. END;
  5201. ELSIF base IS SyntaxTree.MathArrayType THEN
  5202. mathArrayType := base(SyntaxTree.MathArrayType);
  5203. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,value) THEN
  5204. result := Global.NewIntegerValue(system,position,value);
  5205. type := result.type;
  5206. (*
  5207. type := mathArrayType.length.type;
  5208. *)
  5209. ASSERT(type # NIL);
  5210. END;
  5211. END;
  5212. END;
  5213. ELSE
  5214. type := system.lenType;
  5215. END;
  5216. (* ---- FIRST ---- *)
  5217. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5218. type := system.lenType;
  5219. IF CheckRangeType(parameter0) THEN END;
  5220. result.SetAssignable(parameter0.assignable)
  5221. (* ---- LAST ---- *)
  5222. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5223. type := system.lenType;
  5224. IF CheckRangeType(parameter0) THEN END;
  5225. result.SetAssignable(parameter0.assignable)
  5226. (* ---- STEP ---- *)
  5227. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5228. type := system.lenType;
  5229. IF CheckRangeType(parameter0) THEN END;
  5230. result.SetAssignable(parameter0.assignable)
  5231. (* ---- RE ---- *)
  5232. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5233. IF CheckNumberType(parameter0) THEN
  5234. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5235. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5236. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5237. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5238. type := parameter0.type
  5239. ELSE
  5240. type := system.realType
  5241. END
  5242. END;
  5243. result.SetAssignable(parameter0.assignable)
  5244. (* ---- IM ---- *)
  5245. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5246. IF CheckNumberType(parameter0) THEN
  5247. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5248. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5249. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5250. ELSE
  5251. type := system.realType;
  5252. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5253. END
  5254. END;
  5255. result.SetAssignable(parameter0.assignable)
  5256. (* ---- MAX ----- *)
  5257. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5258. IF numberActualParameters = 1 THEN
  5259. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5260. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5261. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5262. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5263. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5264. *)
  5265. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5266. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5267. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5268. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5269. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5270. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5271. END;
  5272. ELSE
  5273. Error(parameter0.position,"is not a type symbol");
  5274. END
  5275. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5276. ConvertOperands(parameter0,parameter1);
  5277. actualParameters.SetExpression(0,parameter0);
  5278. actualParameters.SetExpression(1,parameter1);
  5279. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5280. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5281. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5282. END;
  5283. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5284. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5285. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5286. END;
  5287. END;
  5288. type := parameter0.type;
  5289. ELSE type := SyntaxTree.invalidType;
  5290. END;
  5291. (* ---- MIN ----- *)
  5292. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5293. IF numberActualParameters = 1 THEN
  5294. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5295. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5296. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5297. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType),type(SyntaxTree.IntegerType).signed)));
  5298. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5299. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5300. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),TRUE)));
  5301. ELSIF type IS SyntaxTree.AddressType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType),FALSE)));
  5302. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5303. END;
  5304. ELSE
  5305. Error(parameter0.position,"is not a type symbol");
  5306. END
  5307. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5308. ConvertOperands(parameter0,parameter1);
  5309. actualParameters.SetExpression(0,parameter0);
  5310. actualParameters.SetExpression(1,parameter1);
  5311. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5312. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5313. ELSE result.SetResolved(parameter1.resolved)
  5314. END;
  5315. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5316. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5317. ELSE result.SetResolved(parameter1.resolved)
  5318. END;
  5319. END;
  5320. type := parameter0.type;
  5321. ELSE type := SyntaxTree.invalidType;
  5322. END;
  5323. (* ---- ODD ----- *)
  5324. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5325. type := system.booleanType;
  5326. IF CheckIntegerType(parameter0) THEN
  5327. IF IsIntegerValue(parameter0,i0) THEN
  5328. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5329. type := system.booleanType;
  5330. END;
  5331. END;
  5332. (* ---- ORD ----- *)
  5333. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5334. IF id = Global.Ord THEN
  5335. type := system.integerType;
  5336. ELSE
  5337. type := system.longintType;
  5338. END;
  5339. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5340. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5341. actualParameters.SetExpression(0,parameter0);
  5342. (* IF CheckCharacterType(parameter0) THEN*)
  5343. IF IsCharacterValue(parameter0,c)THEN
  5344. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5345. type := Global.GetSignedIntegerType(system,ORD(c));
  5346. END;
  5347. ELSE Error(parameter0.position, "incompatible parameter");
  5348. END;
  5349. (* ---- SHORT ----- *)
  5350. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5351. type := type0;
  5352. IF IsSignedIntegerType(type) THEN
  5353. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5354. ELSIF type = system.integerType THEN type := system.shortintType
  5355. ELSIF type = system.longintType THEN type := system.integerType
  5356. ELSIF type = system.hugeintType THEN type:= system.longintType
  5357. ELSE
  5358. CASE type.sizeInBits OF
  5359. 16: type := Global.Integer8
  5360. |32: type := Global.Integer16
  5361. |64: type := Global.Integer32
  5362. END;
  5363. END;
  5364. ELSIF IsUnsignedIntegerType(type) THEN
  5365. IF (type.sizeInBits = 8) OR (type = Global.Unsigned8) THEN Error(parameter0.position,"short not applicable")
  5366. ELSE
  5367. CASE type.sizeInBits OF
  5368. 16: type := Global.Unsigned8
  5369. |32: type := Global.Unsigned16
  5370. |64: type := Global.Unsigned32
  5371. END;
  5372. END;
  5373. ELSIF type IS SyntaxTree.FloatType THEN
  5374. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5375. ELSIF type = system.longrealType THEN type := system.realType
  5376. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5377. END;
  5378. ELSIF type IS SyntaxTree.ComplexType THEN
  5379. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5380. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5381. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5382. END;
  5383. ELSE
  5384. Error(parameter0.position,"short not applicable")
  5385. END;
  5386. IF (parameter0.resolved # NIL) THEN
  5387. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5388. IF parameter0 IS SyntaxTree.Value THEN
  5389. result.SetResolved(parameter0(SyntaxTree.Value));
  5390. END;
  5391. END;
  5392. (* ---- LONG ----- *)
  5393. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5394. type := type0;
  5395. IF IsSignedIntegerType(type) THEN
  5396. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5397. ELSIF type = system.longintType THEN type := system.hugeintType
  5398. ELSIF type = system.integerType THEN type := system.longintType
  5399. ELSIF type = system.shortintType THEN type := system.integerType
  5400. ELSE
  5401. CASE type.sizeInBits OF
  5402. 8: type := Global.Integer16
  5403. |16: type := Global.Integer32
  5404. |32: type := Global.Integer64
  5405. END;
  5406. END;
  5407. ELSIF IsUnsignedIntegerType(type) THEN
  5408. IF (type.sizeInBits = 64) OR (type = Global.Unsigned64) THEN Error(parameter0.position,"long not applicable")
  5409. ELSE
  5410. CASE type.sizeInBits OF
  5411. 8: type := Global.Unsigned16
  5412. |16: type := Global.Unsigned32
  5413. |32: type := Global.Unsigned64
  5414. END;
  5415. END;
  5416. ELSIF type IS SyntaxTree.FloatType THEN
  5417. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5418. ELSIF type= system.realType THEN type := system.longrealType
  5419. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5420. END;
  5421. ELSIF type IS SyntaxTree.ComplexType THEN
  5422. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5423. ELSIF type = system.complexType THEN type := system.longcomplexType
  5424. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5425. END;
  5426. ELSE
  5427. Error(parameter0.position,"long not applicable")
  5428. END;
  5429. IF (parameter0.resolved # NIL) THEN
  5430. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5431. IF parameter0 IS SyntaxTree.Value THEN
  5432. result.SetResolved(parameter0(SyntaxTree.Value));
  5433. END;
  5434. END;
  5435. (* ---- SIZE OF ----- *)
  5436. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5437. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5438. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5439. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5440. type := system.integerType;
  5441. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5442. ELSE
  5443. (* for variables, system sizeof could represent the physically occupied size
  5444. determined via the type descriptor, implement that ? *)
  5445. Error(parameter0.position,"is not a type symbol");
  5446. END
  5447. (* ---- SYSTEM.TRACE -----*)
  5448. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5449. FOR i := 0 TO numberActualParameters-1 DO
  5450. parameter0 := actualParameters.GetExpression(i);
  5451. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5452. Error(parameter0.position,"incompatible parameter");
  5453. END;
  5454. END;
  5455. (* remaining issues can only be tested in backend *)
  5456. (* ---- ADDRESSOF----- *)
  5457. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5458. IF HasAddress(parameter0) THEN
  5459. type := system.addressType;
  5460. ELSE
  5461. type := SyntaxTree.invalidType;
  5462. Error(parameter0.position,"has no address");
  5463. END;
  5464. (* ---- BIT ----- *)
  5465. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5466. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5467. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5468. actualParameters.SetExpression(0,parameter0);
  5469. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5470. actualParameters.SetExpression(1,parameter1);
  5471. END;
  5472. type := system.booleanType;
  5473. (* ----- MSK ---- *)
  5474. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5475. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5476. ConvertOperands(parameter0,parameter1);
  5477. actualParameters.SetExpression(0,parameter0);
  5478. actualParameters.SetExpression(1,parameter1);
  5479. END;
  5480. type := parameter0.type;
  5481. (* ---- SYSTEM.GET64 ----- *)
  5482. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5483. IF CheckAddressType(parameter0) THEN
  5484. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5485. actualParameters.SetExpression(0,parameter0);
  5486. END;
  5487. type := system.hugeintType;
  5488. (* ---- SYSTEM.GET32 ----- *)
  5489. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5490. IF CheckAddressType(parameter0) THEN
  5491. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5492. actualParameters.SetExpression(0,parameter0);
  5493. END;
  5494. type := system.longintType;
  5495. (* ---- SYSTEM.GET16 ----- *)
  5496. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5497. IF CheckAddressType(parameter0) THEN
  5498. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5499. actualParameters.SetExpression(0,parameter0);
  5500. END;
  5501. type := system.integerType;
  5502. (* ---- SYSTEM.GET8 ----- *)
  5503. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5504. IF CheckAddressType(parameter0) THEN
  5505. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5506. actualParameters.SetExpression(0,parameter0);
  5507. END;
  5508. type := system.shortintType;
  5509. (* ---- SYSTEM.GetStackPointer ----- *)
  5510. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5511. type := system.addressType;
  5512. (* ---- SYSTEM.GetFramePointer ----- *)
  5513. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5514. type := system.addressType;
  5515. (* ---- SYSTEM.GetActivity ----- *)
  5516. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5517. type := system.objectType;
  5518. (* ---- SYSTEM.SetStackPointer ----- *)
  5519. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5520. IF CheckAddressType(parameter0) THEN
  5521. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5522. actualParameters.SetExpression(0,parameter0);
  5523. END;
  5524. (* ---- SYSTEM.SetFramePointer ----- *)
  5525. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5526. IF CheckAddressType(parameter0) THEN
  5527. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5528. actualParameters.SetExpression(0,parameter0);
  5529. END;
  5530. (* ---- SYSTEM.SetActivity ----- *)
  5531. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5532. IF CheckObjectType(parameter0) THEN
  5533. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5534. actualParameters.SetExpression(0,parameter0);
  5535. END;
  5536. (* ---- LSH, LSL, ROT, ROR ----- *)
  5537. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5538. type := type0;
  5539. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5540. actualParameters.SetExpression(1, parameter1);
  5541. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5542. IF id = Global.Lsh THEN
  5543. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5544. ELSIF id = Global.Rot THEN
  5545. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5546. ELSIF id = Global.Ror THEN
  5547. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5548. END;
  5549. END;
  5550. (* ---- SYSTEM.VAL ----- *)
  5551. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5552. IF CheckTypeDeclarationType(parameter0) THEN
  5553. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5554. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5555. result := SyntaxTree.invalidExpression;
  5556. Error(parameter0.position,"is no basic type");
  5557. ELSE
  5558. IF (parameter1.resolved # NIL) THEN
  5559. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5560. IF parameter0 IS SyntaxTree.Value THEN
  5561. result.SetResolved(parameter0(SyntaxTree.Value));
  5562. END;
  5563. END;
  5564. result.SetAssignable(parameter1.assignable);
  5565. END;
  5566. END;
  5567. (* ---- SYSTEM.GET ----- *)
  5568. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5569. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5570. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5571. actualParameters.SetExpression(0,parameter0);
  5572. END;
  5573. (* ---- SYSTEM.PUT ----- *)
  5574. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5575. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5576. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5577. actualParameters.SetExpression(0,parameter0);
  5578. END;
  5579. (* ---- SYSTEM.PUT64 ----- *)
  5580. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5581. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5582. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5583. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5584. actualParameters.SetExpression(0,parameter0);
  5585. actualParameters.SetExpression(1,parameter1);
  5586. END;
  5587. (* ---- SYSTEM.PUT32 ----- *)
  5588. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5589. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5590. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5591. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5592. actualParameters.SetExpression(0,parameter0);
  5593. actualParameters.SetExpression(1,parameter1);
  5594. END;
  5595. (* ---- SYSTEM.PUT16 ----- *)
  5596. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5597. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5598. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5599. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5600. actualParameters.SetExpression(0,parameter0);
  5601. actualParameters.SetExpression(1,parameter1);
  5602. END;
  5603. (* ---- SYSTEM.PUT8 ----- *)
  5604. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5605. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5606. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5607. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5608. actualParameters.SetExpression(0,parameter0);
  5609. actualParameters.SetExpression(1,parameter1);
  5610. END;
  5611. (* ---- SYSTEM.MOVE ----- *)
  5612. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5613. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5614. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5615. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5616. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5617. actualParameters.SetExpression(0,parameter0);
  5618. actualParameters.SetExpression(1,parameter1);
  5619. actualParameters.SetExpression(2,parameter2);
  5620. END;
  5621. (* ---- SYSTEM.NEW ----- *)
  5622. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5623. IF ~IsPointerType(parameter0.type) THEN
  5624. Error(parameter0.position,"is not a pointer")
  5625. ELSIF CheckSizeType(parameter1) THEN
  5626. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5627. actualParameters.SetExpression(1,parameter1);
  5628. END;
  5629. (* ----SYSTEM.REF ---- *)
  5630. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5631. type := system.addressType
  5632. (* ---- INCR ----- *)
  5633. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5634. type := system.lenType;
  5635. base := type0;
  5636. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5637. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5638. IF i1 < 0 THEN
  5639. Error(position, "invalid dimension");
  5640. base := SyntaxTree.invalidType;
  5641. ELSE
  5642. base := ArrayBase(base,LONGINT(i1)); (* TODO: fix explicit integer truncation *)
  5643. IF (base # NIL) & Indexable(base) THEN
  5644. ELSE
  5645. Error(position, "len on no array");
  5646. IF VerboseErrorMessage THEN
  5647. Printout.Info("base",base);
  5648. END;
  5649. base := SyntaxTree.invalidType;
  5650. END;
  5651. END;
  5652. parameter1 := NewConversion(parameter1.position,parameter1,system.lenType,NIL);
  5653. actualParameters.SetExpression(1,parameter1);
  5654. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5655. mathArrayType := base(SyntaxTree.MathArrayType);
  5656. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5657. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5658. type := system.lenType;
  5659. END;
  5660. END;
  5661. ELSE
  5662. type := system.lenType;
  5663. END;
  5664. (* ---- SUM ----- *)
  5665. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5666. Error(position, "sum operator not applicable");
  5667. (* ---- ALL ----- *)
  5668. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5669. Error(position, "all operator not applicable");
  5670. (* ---- DIM ----- *)
  5671. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5672. type := system.lenType;
  5673. IF type0 IS SyntaxTree.MathArrayType THEN
  5674. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5675. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5676. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5677. END;
  5678. ELSE
  5679. Error(position, "dimension on non math array type");
  5680. END;
  5681. (* ---- CAS ----- *)
  5682. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5683. IF type0.IsComposite () THEN
  5684. Error(position, "first parameter of composite type");
  5685. result := SyntaxTree.invalidExpression;
  5686. ELSIF ~IsVariable (parameter0) THEN
  5687. Error(position, "first parameter not assignable");
  5688. result := SyntaxTree.invalidExpression;
  5689. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5690. Error(position, "second parameter incompatible");
  5691. result := SyntaxTree.invalidExpression;
  5692. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5693. Error(position, "third parameter incompatible");
  5694. result := SyntaxTree.invalidExpression;
  5695. ELSE
  5696. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5697. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5698. type := type0;
  5699. END;
  5700. (* ---- RESHAPE ----- *)
  5701. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5702. IF type0 IS SyntaxTree.MathArrayType THEN
  5703. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5704. base := ArrayBase(type0,MAX(LONGINT));
  5705. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5706. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5707. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5708. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5709. IF ~CompatibleTo(system,type0,parameterType) THEN
  5710. Error(parameter0.position,"incompatible parameter in reshape");
  5711. result := SyntaxTree.invalidExpression;
  5712. ELSE
  5713. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5714. END;
  5715. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5716. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.sizeType);
  5717. IF ~CompatibleTo(system,type1,parameterType) THEN
  5718. Error(parameter1.position,"parameter incompatible to math array of longint");
  5719. result := SyntaxTree.invalidExpression;
  5720. ELSE
  5721. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5722. END;
  5723. ELSE
  5724. Error(position,"reshape on non math array type");
  5725. result := SyntaxTree.invalidExpression;
  5726. END;
  5727. (* ---- SYSTEM.TYPECODE ----- *)
  5728. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5729. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5730. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5731. type := type.resolved;
  5732. IF type IS SyntaxTree.PointerType THEN
  5733. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5734. END;
  5735. IF ~(type IS SyntaxTree.RecordType) THEN
  5736. Error(parameter0.position,"must be type with type descriptor");
  5737. END;
  5738. ELSE
  5739. Error(parameter0.position,"is not a type symbol");
  5740. END;
  5741. type := system.addressType;
  5742. (* -------- FLT --------- *)
  5743. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5744. type := system.realType;
  5745. IF IsRealValue(parameter0, r) THEN
  5746. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5747. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i0) THEN
  5748. value := ABS(i0);
  5749. IF value # 0 THEN
  5750. i1 := 23;
  5751. IF value >= 2*800000H THEN
  5752. REPEAT value := value DIV 2; INC(i1) UNTIL value < 2*800000H;
  5753. ELSIF value < 800000H THEN
  5754. REPEAT value := 2 * value; DEC(i1) UNTIL value >= 800000H;
  5755. END;
  5756. value := (i1 + 127)*800000H - 800000H + value;
  5757. IF i0 < 0 THEN value := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, value) + {31}); END;
  5758. END;
  5759. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, value)));
  5760. END;
  5761. (* ------- CONNECT -------*)
  5762. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5763. (*IF ~(currentIsCellNet) THEN
  5764. Error(position, "connection outside activeCells body block");
  5765. END;*)
  5766. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5767. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5768. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5769. END;
  5770. IF numberActualParameters = 3 THEN
  5771. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5772. Error(position, "incompatible channel size parameter");
  5773. END;
  5774. *)
  5775. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5776. actualParameters.SetExpression(2,parameter2);
  5777. END;
  5778. activeCellsStatement := TRUE;
  5779. (* ---------- DELEGATE --------*)
  5780. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5781. (*
  5782. IF ~(currentIsCellNet) THEN
  5783. Error(position, "connection delegation outside activeCells body block");
  5784. END;
  5785. *)
  5786. IF ~CheckPortType(parameter1, inPort) THEN
  5787. Error(parameter0.position,"not a port")
  5788. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5789. Error(parameter1.position,"not a port")
  5790. ELSIF (outPort.direction # inPort.direction) THEN
  5791. Error(parameter0.position,"invalid port direction");
  5792. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5793. Error(position, "incompatible port sizes");
  5794. END;
  5795. activeCellsStatement := TRUE;
  5796. (* --------- RECEIVE ---------*)
  5797. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5798. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5799. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5800. IF inPort.direction # SyntaxTree.InPort THEN
  5801. Error(parameter0.position,"not an in-port")
  5802. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5803. Error(parameter1.position,"incompatible to port type");
  5804. END;
  5805. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5806. IF ~SameType(parameter2.type, system.integerType) THEN
  5807. Error(parameter2.position,"incompatible to integer type");
  5808. END;
  5809. END;
  5810. END;
  5811. (* --------- SEND ---------*)
  5812. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5813. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5814. IF CheckPortType(parameter0,outPort) THEN
  5815. IF outPort.direction # SyntaxTree.OutPort THEN
  5816. Error(parameter1.position,"not an out-port")
  5817. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5818. Error(parameter1.position,"incompatible to port type");
  5819. ELSE
  5820. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5821. actualParameters.SetExpression(1,parameter1);
  5822. END;
  5823. END;
  5824. (* ------- custom builtins ----- *)
  5825. ELSIF id = Global.systemSpecial THEN
  5826. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5827. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5828. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5829. type := procedureType.returnType;
  5830. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5831. (* go through all formal parameters *)
  5832. formalParameter := procedureType.firstParameter;
  5833. FOR i := 0 TO actualParameters.Length() - 1 DO
  5834. actualParameter := actualParameters.GetExpression(i);
  5835. IF actualParameter = SyntaxTree.invalidExpression THEN
  5836. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5837. Error(position, "incompatible parameter")
  5838. ELSE
  5839. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5840. END;
  5841. actualParameters.SetExpression(i, actualParameter);
  5842. formalParameter := formalParameter.nextParameter
  5843. END
  5844. END
  5845. ELSE
  5846. Error(position, "builtin not implemented");
  5847. result := SyntaxTree.invalidExpression;
  5848. END;
  5849. END;
  5850. IF result # SyntaxTree.invalidExpression THEN
  5851. type := ResolveType(type);
  5852. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5853. result.SetType(type);
  5854. END;
  5855. RETURN result
  5856. END NewBuiltinCallDesignator;
  5857. (** return type guard designator left(type)
  5858. - check if type can be extended (i.e. is no static record)
  5859. - check if type is a type extension of left.type
  5860. - returns new type guard designator
  5861. returns invalidDesignator = invalidExpression if error
  5862. **)
  5863. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5864. VAR result: SyntaxTree.Designator;
  5865. BEGIN
  5866. result := SyntaxTree.invalidDesignator;
  5867. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5868. Error(position, "no type extension of type");
  5869. IF VerboseErrorMessage THEN
  5870. Printout.Info("left",left);
  5871. Printout.Info("type",type);
  5872. END;
  5873. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5874. Error(position, "variable cannot be extended");
  5875. ELSIF IsUnsafePointer(left.type) THEN
  5876. Error(position, "forbidden type guard on unsafe pointer");
  5877. ELSE
  5878. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5879. result.SetType(type);
  5880. result.SetAssignable(left.assignable);
  5881. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5882. END;
  5883. RETURN result
  5884. END NewTypeGuardDesignator;
  5885. (** check and resolve parameter designator left(expression list)
  5886. - check expression list
  5887. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5888. - elsif left is a procedure type then
  5889. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5890. - else return is a procedure call then return ProcedureCallDesignator
  5891. returns invalidDesignator = invalidExpression if error
  5892. **)
  5893. PROCEDURE VisitParameterDesignator*(designator: SyntaxTree.ParameterDesignator);
  5894. VAR
  5895. parameters: SyntaxTree.ExpressionList;
  5896. left: SyntaxTree.Designator;
  5897. result,expression: SyntaxTree.Expression;
  5898. typeDeclaration: SyntaxTree.TypeDeclaration;
  5899. type, expressionType: SyntaxTree.Type;
  5900. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5901. BEGIN
  5902. type := type.resolved;
  5903. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5904. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5905. END;
  5906. RETURN type
  5907. END BaseType;
  5908. BEGIN
  5909. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5910. result := SyntaxTree.invalidDesignator;
  5911. left := ResolveDesignator(designator.left);
  5912. IF left # SyntaxTree.invalidDesignator THEN
  5913. parameters := designator.parameters;
  5914. IF ExpressionList(parameters) THEN
  5915. IF (left.type = NIL) THEN
  5916. Error(left.position,"object is not a procedure or cannot be extended");
  5917. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5918. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5919. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5920. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5921. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5922. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5923. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5924. ELSE
  5925. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5926. END
  5927. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5928. expression := parameters.GetExpression(0);
  5929. type := typeDeclaration.declaredType.resolved;
  5930. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5931. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5932. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5933. OR (expressionType IS SyntaxTree.EnumerationType)
  5934. ) OR ((type IS SyntaxTree.SetType) & (expressionType IS SyntaxTree.SetType)) THEN
  5935. IF ((type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.IntegerType) & (~type(SyntaxTree.IntegerType).signed)) & (expressionType IS SyntaxTree.FloatType) THEN
  5936. Error(left.position,"invalid unsigned type in explicit conversion");
  5937. ELSE
  5938. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5939. END;
  5940. ELSE
  5941. Error(left.position,"invalid type in explicit conversion");
  5942. END;
  5943. ELSE
  5944. Error(left.position,"called object is not a procedure or cannot be extended");
  5945. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5946. result := SyntaxTree.invalidDesignator;
  5947. END;
  5948. ELSE
  5949. result := SyntaxTree.invalidDesignator
  5950. END;
  5951. END;
  5952. resolvedExpression := result;
  5953. END VisitParameterDesignator;
  5954. (** check dereference designator left^
  5955. - check if left is pointer type or left is object type
  5956. - return new dereference designator with type = left.baseType.type (if appropriate)
  5957. with error handling
  5958. returns invalidDesignator = invalidExpression if error
  5959. **)
  5960. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5961. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5962. BEGIN
  5963. result := SyntaxTree.invalidDesignator;
  5964. type := left.type;
  5965. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5966. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5967. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5968. result.SetAssignable(TRUE);
  5969. result.SetType(type);
  5970. result.SetHidden(left.isHidden);
  5971. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5972. type := type.resolved;
  5973. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5974. result.SetAssignable(TRUE);
  5975. result.SetType(type);
  5976. result.SetHidden(left.isHidden);
  5977. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5978. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5979. result.SetAssignable(TRUE);
  5980. result.SetType(type);
  5981. result.SetHidden(left.isHidden);
  5982. ELSE
  5983. Error(position, "dereference on no pointer");
  5984. IF VerboseErrorMessage THEN
  5985. Printout.Info("pointer", type);
  5986. Printout.Info("scope", currentScope);
  5987. END;
  5988. END;
  5989. RETURN result
  5990. END NewDereferenceDesignator;
  5991. (** check supercall designator left^
  5992. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5993. - return new supercall designator with type = left.type
  5994. with error handling
  5995. **)
  5996. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5997. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5998. objectScope: SyntaxTree.Scope;
  5999. BEGIN
  6000. result := SyntaxTree.invalidDesignator;
  6001. IF left = SyntaxTree.invalidDesignator THEN
  6002. (* error already handled *)
  6003. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  6004. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  6005. ASSERT(symbol # SyntaxTree.invalidSymbol);
  6006. IF symbol IS SyntaxTree.Procedure THEN
  6007. procedure := symbol(SyntaxTree.Procedure);
  6008. objectScope := currentScope;
  6009. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  6010. objectScope := objectScope.outerScope;
  6011. END;
  6012. IF (left.left = NIL) OR ~
  6013. (
  6014. (left.left IS SyntaxTree.SelfDesignator) OR
  6015. (left.left IS SyntaxTree.DereferenceDesignator)
  6016. & (left.left(SyntaxTree.Designator).left # NIL)
  6017. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  6018. Error(position, "procedure not in immediate object scope");
  6019. IF VerboseErrorMessage THEN
  6020. Printout.Info("left.left",left.left);
  6021. END;
  6022. ELSIF procedure.super # NIL THEN
  6023. result := SyntaxTree.NewSupercallDesignator(position,left);
  6024. result.SetType(left.type.resolved)
  6025. ELSE
  6026. Error(position, "no supermethod for this procedure");
  6027. END;
  6028. ELSE
  6029. Error(position, "symbol is not a procedure");
  6030. END;
  6031. ELSE
  6032. Error(position, "is no symbol designator");
  6033. END;
  6034. RETURN result
  6035. END NewSupercallDesignator;
  6036. (** check and semantically resolve arrow designator left^
  6037. - if left is procedure type -> result := SupercallDesignator
  6038. - else result := DereferenceDesignator
  6039. returns result via global variable resolvedExpression
  6040. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  6041. **)
  6042. PROCEDURE VisitArrowDesignator*(arrowDesignator: SyntaxTree.ArrowDesignator);
  6043. VAR left: SyntaxTree.Designator;
  6044. BEGIN
  6045. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  6046. left := ResolveDesignator(arrowDesignator.left);
  6047. IF left # NIL THEN
  6048. IF (left.type = NIL) THEN
  6049. Error(arrowDesignator.position,"Invalid arrow designator");
  6050. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  6051. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  6052. ELSE
  6053. IF IsPointerToObject(left.type) THEN
  6054. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  6055. END;
  6056. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  6057. END
  6058. END
  6059. END VisitArrowDesignator;
  6060. (** check and return expression
  6061. - if expression has no type then resolve expression
  6062. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  6063. - return result
  6064. **)
  6065. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6066. VAR result,prev: SyntaxTree.Expression;
  6067. BEGIN
  6068. IF expression = NIL THEN result := NIL
  6069. ELSIF (expression.type = NIL) THEN
  6070. prev := resolvedExpression;
  6071. resolvedExpression := SyntaxTree.invalidExpression;
  6072. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  6073. expression.SetType(SyntaxTree.invalidType);
  6074. END;
  6075. VExpression(expression);
  6076. result := resolvedExpression;
  6077. IF currentIsRealtime THEN
  6078. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  6079. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  6080. END;
  6081. END;
  6082. (* designator modifiers for backends if they support it ...*)
  6083. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  6084. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  6085. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  6086. END;
  6087. resolvedExpression := prev
  6088. ELSE
  6089. result := expression
  6090. END;
  6091. RETURN result
  6092. END ResolveExpression;
  6093. (**
  6094. check expression to be constant expression
  6095. - resolve expression
  6096. - if valid then check that of value type
  6097. report error and return invalidExpression if anything fails
  6098. **)
  6099. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6100. VAR position: Position;
  6101. BEGIN
  6102. position := expression.position;
  6103. expression := ResolveExpression(expression);
  6104. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6105. ELSIF (expression.resolved = NIL) THEN
  6106. Error(position, "expression is not constant");
  6107. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6108. expression := SyntaxTree.invalidExpression;
  6109. END;
  6110. RETURN expression
  6111. END ConstantExpression;
  6112. (** check expression to be constant integer
  6113. - resolve expresssion
  6114. - if valid then check that of integer value type
  6115. report error and return invalidExpression if anything fails
  6116. **)
  6117. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6118. VAR position: Position;
  6119. BEGIN
  6120. position := expression.position;
  6121. expression := ResolveExpression(expression);
  6122. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6123. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6124. expression := SyntaxTree.invalidExpression;
  6125. Error(position, "expression is not a constant integer");
  6126. END;
  6127. RETURN expression
  6128. END ConstantInteger;
  6129. (** check expression as positive (>=0) constant integer
  6130. - resolve expression
  6131. - if valid then check that integer value
  6132. - if integer value then check that value >= 0
  6133. report error and return invalidExpression if anything fails
  6134. **)
  6135. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6136. VAR position: Position;
  6137. BEGIN
  6138. position := expression.position;
  6139. expression := ConstantExpression(expression);
  6140. IF expression = SyntaxTree.invalidExpression THEN
  6141. (* error already reported *)
  6142. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6143. Error(position, "expression is not integer valued");
  6144. expression := SyntaxTree.invalidExpression
  6145. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value <0) THEN
  6146. Error(position, "integer is not greater or equal zero");
  6147. END;
  6148. RETURN expression
  6149. END ConstantIntegerGeq0;
  6150. (** check expression as condition
  6151. - resolve expression
  6152. - if valid expression then check that result type is boolean
  6153. report error and return invalidExpression if anything fails
  6154. **)
  6155. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6156. VAR position: Position;
  6157. BEGIN
  6158. position := expression.position;
  6159. expression := ResolveExpression(expression);
  6160. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6161. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6162. expression := SyntaxTree.invalidExpression;
  6163. Error(position, "expression is not boolean");
  6164. END;
  6165. RETURN expression
  6166. END ResolveCondition;
  6167. (*** symbols ***)
  6168. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6169. BEGIN
  6170. VSymbol(x);
  6171. END ResolveSymbol;
  6172. (** check a symbol
  6173. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6174. **)
  6175. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6176. VAR scope: SyntaxTree.Scope;
  6177. BEGIN
  6178. (* visibility *)
  6179. scope := symbol.scope;
  6180. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6181. scope := scope.outerScope;
  6182. END;
  6183. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6184. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6185. Error(symbol.position,"cannot be exported");
  6186. IF VerboseErrorMessage THEN
  6187. Printout.Info("symbol",symbol);
  6188. END;
  6189. END;
  6190. END;
  6191. END CheckSymbolVisibility;
  6192. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6193. If node is currently being resolved then emit a cyclic definition error.
  6194. Return TRUE only if node is fully resolved.
  6195. **)
  6196. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6197. VAR result: BOOLEAN;
  6198. BEGIN
  6199. IF SyntaxTree.Resolved IN x.state THEN
  6200. result := FALSE
  6201. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6202. Error(x.position,"cyclic definition");
  6203. result := FALSE;
  6204. ELSE
  6205. result := TRUE;
  6206. x.SetState(SyntaxTree.BeingResolved)
  6207. END;
  6208. RETURN result
  6209. END SymbolNeedsResolution;
  6210. (** check and resolve a type declaration symbol = Type
  6211. - set type to declaration type
  6212. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6213. This is so because the type declaration itself does not have a type but it only stands for a type.
  6214. In the implementation of the compiler this made a lot much easier.
  6215. - resolve and set declared type
  6216. - check symbol
  6217. **)
  6218. PROCEDURE VisitTypeDeclaration*(typeDeclaration: SyntaxTree.TypeDeclaration);
  6219. VAR prevScope: SyntaxTree.Scope;
  6220. BEGIN
  6221. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6222. IF SymbolNeedsResolution(typeDeclaration) THEN
  6223. typeDeclaration.SetState(SyntaxTree.Resolved);
  6224. prevScope := currentScope;
  6225. currentScope := typeDeclaration.scope;
  6226. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6227. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6228. CheckSymbolVisibility(typeDeclaration);
  6229. typeDeclaration.SetState(SyntaxTree.Resolved);
  6230. currentScope := prevScope;
  6231. END;
  6232. END VisitTypeDeclaration;
  6233. (** check and resolve a constant declaration symbol = (constant) expression
  6234. - check expression
  6235. - set type and value
  6236. - check symbol
  6237. **)
  6238. PROCEDURE VisitConstant*(constant: SyntaxTree.Constant);
  6239. VAR
  6240. expression: SyntaxTree.Expression;
  6241. type: SyntaxTree.Type;
  6242. name: Basic.SegmentedName;
  6243. replacement: Replacement;
  6244. BEGIN
  6245. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6246. IF SymbolNeedsResolution(constant) THEN
  6247. expression := constant.value;
  6248. IF replacements # NIL THEN
  6249. Global.GetSymbolSegmentedName(constant, name);
  6250. replacement := replacements;
  6251. WHILE (replacement # NIL) & (replacement.name # name) DO
  6252. replacement := replacement.next;
  6253. END;
  6254. IF replacement # NIL THEN
  6255. InfoSS(constant.position, "replacing constant", constant.name);
  6256. (*
  6257. NEW(stringReader, Strings.Length(replacement.string^));
  6258. stringReader.Set(replacement.string^);
  6259. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6260. NEW(parser, scanner, diagnostics);
  6261. expression := parser.Expression();
  6262. *)
  6263. expression := replacement.expression;
  6264. replacement.used := TRUE;
  6265. END;
  6266. END;
  6267. constant.SetType(SyntaxTree.invalidType);
  6268. expression := ConstantExpression(expression);
  6269. ASSERT(expression.type # NIL);
  6270. type := expression.type.resolved;
  6271. constant.SetType(type);
  6272. constant.SetValue(expression);
  6273. CheckSymbolVisibility(constant);
  6274. constant.SetState(SyntaxTree.Resolved);
  6275. END;
  6276. END VisitConstant;
  6277. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6278. VAR procedureAlignment: LONGINT;
  6279. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6280. (* least common multiple *)
  6281. VAR a,b: LONGINT;
  6282. BEGIN
  6283. a := a0; b := b0;
  6284. WHILE (a # b) DO
  6285. IF a < b THEN a := a+a0
  6286. ELSE b := b + b0
  6287. END;
  6288. END;
  6289. RETURN a
  6290. END LCM;
  6291. BEGIN
  6292. IF alignment > 1 THEN
  6293. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6294. IF (procedureAlignment > 1) THEN
  6295. alignment := LCM(alignment, procedureAlignment);
  6296. END;
  6297. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6298. END;
  6299. END AdaptStackAlignment;
  6300. (** check and resolve a variable / field
  6301. - check and set type
  6302. - negative check on open array type
  6303. - check symbol
  6304. **)
  6305. PROCEDURE VisitVariable*(variable: SyntaxTree.Variable);
  6306. VAR modifiers: SyntaxTree.Modifier; value: Basic.Integer; position: Position; pointerType: SyntaxTree.PointerType;
  6307. BEGIN
  6308. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6309. IF SymbolNeedsResolution(variable) THEN
  6310. modifiers := variable.modifiers;
  6311. (*
  6312. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6313. variable.AddFlags(flags);
  6314. *)
  6315. variable.SetType(ResolveType(variable.type));
  6316. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6317. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6318. Error(variable.position,"forbidden open array variable");
  6319. END;
  6320. END;
  6321. CheckSymbolVisibility(variable);
  6322. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6323. variable.SetUntraced(TRUE);
  6324. IF ~ContainsPointer(variable.type) THEN
  6325. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6326. Error(position, "untraced flag on non-pointer variable");
  6327. END;
  6328. END;
  6329. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6330. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6331. IF ~PowerOf2(value) THEN
  6332. Error(position, "forbidden alignment - must be power of two");
  6333. ELSE
  6334. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6335. END;
  6336. END;
  6337. variable.SetAlignment(FALSE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6338. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6339. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6340. Error(position, "fixed position not possible in procedure");
  6341. END;
  6342. variable.SetAlignment(TRUE,LONGINT(value)); (* TODO: fix explicit integer truncation *)
  6343. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6344. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6345. Error(position,"fictive offset not possible in procedure");
  6346. END;
  6347. variable.SetFictive(LONGINT (value)); (* TODO: fix explicit integer truncation *)
  6348. variable.SetOffset(LONGINT(value)*system.dataUnit); (* TODO: fix explicit integer truncation *)
  6349. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6350. END;
  6351. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6352. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6353. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6354. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6355. END;
  6356. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6357. IF variable.initializer # NIL THEN
  6358. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6359. END;
  6360. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6361. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6362. pointerType.SetPointerBase(variable.type);
  6363. pointerType.SetHidden(TRUE);
  6364. variable.SetType(ResolveType(pointerType));
  6365. END;
  6366. variable.SetState(SyntaxTree.Resolved);
  6367. END;
  6368. END VisitVariable;
  6369. PROCEDURE VisitProperty*(property: SyntaxTree.Property);
  6370. BEGIN
  6371. VisitVariable(property)
  6372. END VisitProperty;
  6373. (** check and resolve a (procedure) parameter
  6374. - check and set type
  6375. - check symbol
  6376. - check parameter kind and set read-only flags if appropriate
  6377. **)
  6378. PROCEDURE VisitParameter*(parameter: SyntaxTree.Parameter);
  6379. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6380. BEGIN
  6381. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6382. IF SymbolNeedsResolution(parameter) THEN
  6383. modifiers := parameter.modifiers;
  6384. parameter.SetType(ResolveType(parameter.type));
  6385. ASSERT(parameter.type.resolved # NIL);
  6386. CheckSymbolVisibility(parameter);
  6387. IF parameter.defaultValue # NIL THEN
  6388. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6389. Error(parameter.position,"forbidden default value on non-value parameter");
  6390. ELSE
  6391. expression := ConstantExpression(parameter.defaultValue);
  6392. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6393. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6394. parameter.SetDefaultValue(expression);
  6395. END;
  6396. END;
  6397. END;
  6398. IF (parameter.kind = SyntaxTree.ValueParameter) & IsMathArrayType(parameter.type)THEN
  6399. Error(parameter.position, "forbidden value parameter of math array type ");
  6400. END;
  6401. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6402. parameter.SetUntraced(TRUE);
  6403. IF ~ContainsPointer(parameter.type) THEN
  6404. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6405. Error(position, "untraced flag on non-pointer variable");
  6406. END;
  6407. END;
  6408. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6409. parameter.SetMoveable(TRUE);
  6410. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6411. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6412. Error(position, "illegal movable flag on non-address variable");
  6413. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6414. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6415. Error(position, "unnecessary movable flag on variable variable");
  6416. END;
  6417. END;
  6418. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6419. parameter.SetState(SyntaxTree.Resolved);
  6420. END;
  6421. END VisitParameter;
  6422. (** check and resolve a procedure (with declaration and implementation scope)
  6423. - check the procedure type
  6424. - check if method (i.e. in record scope), if so then
  6425. - check if (unique) constructor
  6426. - check if (unique) finalizer
  6427. - check if super method available, if so then check signature
  6428. - of not in record scope then negative check on constructor flag
  6429. - of not in record scope then negative check on finalizer flag
  6430. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6431. - check procedure symbol
  6432. **)
  6433. PROCEDURE VisitProcedure*(procedure: SyntaxTree.Procedure);
  6434. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6435. procedureType: SyntaxTree.ProcedureType;
  6436. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6437. qualifiedType: SyntaxTree.QualifiedType;
  6438. value: Basic.Integer;
  6439. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6440. position: Position;
  6441. fp: SyntaxTree.FingerPrint;
  6442. BEGIN
  6443. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6444. IF IsOberonInline(procedure) THEN
  6445. IF SyntaxTree.Public * procedure.access # {} THEN
  6446. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6447. END;
  6448. procedure.SetInline(FALSE);
  6449. procedure.SetOberonInline(TRUE);
  6450. END;
  6451. IF SymbolNeedsResolution(procedure) THEN
  6452. recentIsRealtime := currentIsRealtime;
  6453. recentIsBodyProcedure := currentIsBodyProcedure;
  6454. CheckSymbolVisibility(procedure);
  6455. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6456. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6457. modifiers := procedureType.modifiers;
  6458. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN
  6459. procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6460. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6461. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6462. END;
  6463. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6464. procedureType.SetInterrupt(TRUE);
  6465. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6466. END;
  6467. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6468. procedureType.SetNoReturn(TRUE);
  6469. END;
  6470. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(LONGINT(value)) (* TODO: fix explicit integer truncation *) END;
  6471. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6472. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6473. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6474. END;
  6475. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6476. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6477. END;
  6478. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6479. IF ~PowerOf2(value) THEN
  6480. Error(position, "forbidden stack alignment - must be power of two");
  6481. ELSE
  6482. procedureType.SetStackAlignment(LONGINT(value)) (* TODO: fix explicit integer truncation *)
  6483. END;
  6484. END;
  6485. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6486. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6487. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6488. END;
  6489. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6490. SyntaxTree.InitFingerPrint(fp);
  6491. fp.shallow := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6492. fp.public := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6493. fp.private := LONGINT(value); (* TODO: fix explicit integer truncation *)
  6494. fp.shallowAvailable := TRUE;
  6495. procedure.SetFingerPrint(fp);
  6496. END;
  6497. CheckModifiers(modifiers, TRUE);
  6498. modifiers := procedureType.returnTypeModifiers;
  6499. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6500. CheckModifiers(modifiers, TRUE);
  6501. procedure.SetState(SyntaxTree.Resolved);
  6502. FixProcedureType(procedureType);
  6503. currentIsRealtime := procedureType.isRealtime;
  6504. currentIsBodyProcedure := procedure.isBodyProcedure;
  6505. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6506. THEN
  6507. Error(procedure.position,"problems during parameter offset computation");
  6508. END;
  6509. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6510. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6511. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6512. Error(procedure.position,"problem during parameter offset generation");
  6513. END;
  6514. END;
  6515. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6516. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6517. procedureType.SetDelegate(TRUE);
  6518. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6519. (* add auto-self *)
  6520. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6521. IF (record.pointerType.typeDeclaration = NIL) THEN
  6522. selfParameter.SetType(record.pointerType);
  6523. ELSE
  6524. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6525. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6526. qualifiedType.SetResolved(record.pointerType);
  6527. selfParameter.SetType(qualifiedType);
  6528. END;
  6529. selfParameter.SetAccess(SyntaxTree.Hidden);
  6530. ELSIF (procedureType.selfParameter = NIL) THEN
  6531. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.VarParameter);
  6532. IF (record.typeDeclaration = NIL) THEN
  6533. selfParameter.SetType(record);
  6534. ELSE
  6535. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.typeDeclaration.name);
  6536. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6537. qualifiedType.SetResolved(record);
  6538. selfParameter.SetType(qualifiedType);
  6539. END;
  6540. procedureType.SetSelfParameter(selfParameter);
  6541. END;
  6542. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6543. IF procedure.isConstructor THEN
  6544. (*! constructor is always visible, compatibility to paco
  6545. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6546. *)
  6547. procedure.MarkUsed;
  6548. IF procedureType.returnType # NIL THEN
  6549. Error(procedure.position,"constructor with forbidden return type");
  6550. END;
  6551. proc := procedure.scope.firstProcedure;
  6552. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6553. proc := proc.nextProcedure;
  6554. END;
  6555. IF proc # NIL THEN
  6556. Error(procedure.position,"duplicate constructor")
  6557. ELSE
  6558. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6559. END;
  6560. END;
  6561. IF procedure.isFinalizer THEN
  6562. procedure.MarkUsed;
  6563. IF procedureType.returnType # NIL THEN
  6564. Error(procedure.position,"finalizer with forbidden return type");
  6565. END;
  6566. IF procedureType.numberParameters # 0 THEN
  6567. Error(procedure.position,"finalizer with formal parameters");
  6568. END;
  6569. proc := procedure.scope.firstProcedure;
  6570. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6571. proc := proc.nextProcedure;
  6572. END;
  6573. IF proc # NIL THEN
  6574. Error(procedure.position,"duplicate finalizer")
  6575. ELSE
  6576. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6577. END;
  6578. END;
  6579. super := FindSuperProcedure(record.recordScope, procedure);
  6580. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6581. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6582. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6583. END;
  6584. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6585. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6586. END;
  6587. IF super.isFinal THEN
  6588. Error(procedure.position,"forbidden method extending final method");
  6589. END;
  6590. (*
  6591. IF super.access # procedure.access THEN
  6592. Warning(procedure.position, "forbiden visibility mismatch of method and super method");
  6593. END;
  6594. *)
  6595. procedure.SetSuper(super);
  6596. super.SetOverwritten(TRUE);
  6597. procedure.SetAccess(procedure.access+super.access);
  6598. procedure.MarkUsed;
  6599. END;
  6600. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6601. THEN
  6602. Error(procedure.position,"problems during parameter offset computation");
  6603. END;
  6604. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6605. IF cellsAreObjects THEN
  6606. procedureType.SetDelegate(TRUE);
  6607. END;
  6608. IF procedure.isConstructor THEN
  6609. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6610. END;
  6611. ELSIF procedure.isConstructor THEN
  6612. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6613. END;
  6614. Declarations(procedure.procedureScope, FALSE, {0,1});
  6615. (* body resolution part done as late fix of the procedure type *)
  6616. procedure.SetState(SyntaxTree.Resolved);
  6617. currentIsRealtime := recentIsRealtime;
  6618. currentIsBodyProcedure := recentIsBodyProcedure;
  6619. END;
  6620. END VisitProcedure;
  6621. (**
  6622. a builtin procedure is a global item that may not be modified locally
  6623. instead the resolving of builtin procedure calls are done in the esignator
  6624. **)
  6625. PROCEDURE VisitBuiltin*(builtinProcedure: SyntaxTree.Builtin);
  6626. VAR type: SyntaxTree.Type;
  6627. BEGIN
  6628. type := ResolveType(builtinProcedure.type);
  6629. END VisitBuiltin;
  6630. (* nopov *)
  6631. (** check and resolve operator
  6632. - operators are first checked as procedures
  6633. - then additional operator-specific checks are done
  6634. - note that only module-scope operators are checked here
  6635. (operators in a record scope are only allowed in the context of
  6636. array-structured object types and checked in 'ResolveArrayStructure')
  6637. - also note that inter-operator conformity is not checked here
  6638. **)
  6639. PROCEDURE VisitOperator*(operator: SyntaxTree.Operator);
  6640. VAR
  6641. procedureType: SyntaxTree.ProcedureType;
  6642. leftType, rightType: SyntaxTree.Type;
  6643. identifierNumber: LONGINT; position: Position;
  6644. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6645. modifiers: SyntaxTree.Modifier;
  6646. (** whether a type is locally defined in the current module scope
  6647. for arrays, the base type must be locally defined **)
  6648. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6649. BEGIN
  6650. IF type = NIL THEN
  6651. RETURN FALSE
  6652. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6653. RETURN TRUE
  6654. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6655. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6656. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6657. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6658. ELSE
  6659. RETURN FALSE
  6660. END
  6661. END IsLocallyDefined;
  6662. BEGIN
  6663. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6664. procedureType := operator.type(SyntaxTree.ProcedureType);
  6665. modifiers := procedureType.modifiers;
  6666. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6667. CheckModifiers(modifiers, TRUE);
  6668. VisitProcedure(operator);
  6669. IF operator.scope IS SyntaxTree.RecordScope THEN
  6670. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6671. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6672. IF identifierNumber = -1 THEN
  6673. Error(operator.position, "operator with unknown identifier")
  6674. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6675. Error(operator.position, "identifier may not be used for operator")
  6676. ELSE
  6677. IF procedureType.numberParameters < 1 THEN
  6678. Error(operator.position, "operator without operand");
  6679. ELSIF procedureType.numberParameters > 2 THEN
  6680. Error(operator.position, "operator with more than two operands");
  6681. ELSE
  6682. (* determine operand types *)
  6683. leftType := procedureType.firstParameter.type;
  6684. IF procedureType.numberParameters > 1 THEN
  6685. rightType := procedureType.firstParameter.nextParameter.type
  6686. ELSE
  6687. rightType := NIL
  6688. END;
  6689. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6690. IF (currentScope.ownerModule.name # Global.ArrayBaseName) & (currentScope.ownerModule.name # Global.ComplexNumbersName) THEN
  6691. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6692. Error(operator.position, "none of the operands is declared in the same module")
  6693. END
  6694. END;
  6695. (* TODO: refine the checks, think about how restrictive the checks should be
  6696. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6697. They might be used for intersection, union, complement of custom object types *)
  6698. (* defaults *)
  6699. hasReturnType := TRUE;
  6700. mustBeUnary := FALSE;
  6701. mustBeBinary := FALSE;
  6702. mustReturnBoolean := FALSE;
  6703. mustReturnInteger := FALSE;
  6704. mustHaveEquitypedOperands := FALSE;
  6705. (* operator-specific exceptions *)
  6706. CASE identifierNumber OF
  6707. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6708. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6709. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6710. mustBeBinary := TRUE
  6711. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6712. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6713. | Scanner.Times: mustBeBinary := TRUE
  6714. | Scanner.TimesTimes: mustBeBinary := TRUE
  6715. | Scanner.DotTimes: mustBeBinary := TRUE
  6716. | Scanner.PlusTimes: mustBeBinary := TRUE
  6717. | Scanner.Slash: mustBeBinary := TRUE
  6718. | Scanner.Backslash: mustBeBinary := TRUE
  6719. | Scanner.DotSlash: mustBeBinary := TRUE
  6720. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6721. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6722. | Scanner.Not: mustBeUnary := TRUE
  6723. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6724. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6725. | Scanner.Transpose: mustBeUnary := TRUE;
  6726. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6727. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6728. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6729. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6730. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6731. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6732. | Global.Abs: mustBeUnary := TRUE;
  6733. | Global.Ash: (* TODO: arity? *)
  6734. | Global.Cap: (* TODO: arity? *)
  6735. | Global.Chr: mustBeUnary := TRUE;
  6736. | Global.Entier: (* TODO: arity? *)
  6737. | Global.EntierH: (* TODO: arity? *)
  6738. | Global.Len: (* unary and binary *)
  6739. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6740. | Global.Max, Global.Min: (* unary and binary *)
  6741. | Global.Odd: (* TODO: arity? *)
  6742. | Global.Sum: (* TODO: arity? *)
  6743. | Global.All: (* TODO: arity? *)
  6744. | Global.Re, Global.Im:
  6745. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6746. | Scanner.Alias:
  6747. | Scanner.GreaterGreater, Scanner.LessLess:
  6748. mustBeBinary := TRUE; hasReturnType := FALSE;
  6749. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6750. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6751. END;
  6752. (* check parameter count *)
  6753. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6754. Error(operator.position,"operator is not unary")
  6755. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6756. Error(operator.position,"operator is not binary")
  6757. END;
  6758. (* check parameter types *)
  6759. (* TODO: is this used at all? *)
  6760. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6761. leftType := procedureType.firstParameter.type;
  6762. rightType := procedureType.firstParameter.nextParameter.type;
  6763. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6764. Error(operator.position, "the two operands are not of the same type")
  6765. END
  6766. END;
  6767. (* check return type *)
  6768. IF hasReturnType THEN
  6769. IF procedureType.returnType = NIL THEN
  6770. Error(operator.position, "return type required")
  6771. ELSIF mustReturnBoolean THEN
  6772. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6773. Error(operator.position,"return type is not Boolean")
  6774. END
  6775. ELSIF mustReturnInteger THEN
  6776. IF ~IsSizeType(procedureType.returnType, system.addressSize) THEN
  6777. Error(operator.position,"return type is no size type")
  6778. END
  6779. END
  6780. ELSIF procedureType.returnType # NIL THEN
  6781. Error(operator.position, "return type not allowed")
  6782. END
  6783. END
  6784. END
  6785. END
  6786. END VisitOperator;
  6787. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6788. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6789. BEGIN
  6790. IF error THEN RETURN FALSE END;
  6791. prevScope := currentScope;
  6792. prevDiagnostics := diagnostics;
  6793. diagnostics := NIL; (* suppress error output *)
  6794. currentScope := module.moduleScope;
  6795. VisitImport(x);
  6796. IF ~error THEN
  6797. module.moduleScope.AddImport(x);
  6798. x.SetScope(module.moduleScope);
  6799. END;
  6800. currentScope := prevScope;
  6801. diagnostics := prevDiagnostics;
  6802. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6803. END AddImport;
  6804. (** check and resolve import
  6805. - check for name = SYSTEM
  6806. - check for forbidden self import
  6807. - search through global import cache: already imported?
  6808. - check if already imported indirectly
  6809. - import if necessary -> set module and enter into import cache
  6810. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6811. - after this import this direct import and all indirect imports are stored in the current module's import list
  6812. **)
  6813. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  6814. VAR
  6815. module: SyntaxTree.Module;
  6816. moduleScope: SyntaxTree.ModuleScope;
  6817. import,reimport: SyntaxTree.Import;
  6818. filename: FileName;
  6819. prevScope: SyntaxTree.Scope;
  6820. BEGIN
  6821. IF SymbolNeedsResolution(x) THEN
  6822. prevScope := currentScope;
  6823. x.SetType(SyntaxTree.importType);
  6824. moduleScope := currentScope.ownerModule.moduleScope;
  6825. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6826. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6827. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6828. Error(x.position,"forbidden self import");
  6829. ELSE
  6830. (* search through global import list: already imported ? *)
  6831. IF (x.module = NIL) & (importCache # NIL) THEN
  6832. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6833. ELSE import := NIL
  6834. END;
  6835. IF x.module # NIL THEN (* already imported indirectly *)
  6836. module := x.module;
  6837. ELSIF import # NIL THEN (* already in module list *)
  6838. module := import.module;
  6839. ASSERT(module # NIL);
  6840. x.SetModule(module);
  6841. ELSE (* must be imported *)
  6842. Global.ModuleFileName(x.moduleName,x.context,filename);
  6843. IF symbolFileFormat # NIL THEN
  6844. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6845. IF module = NIL THEN
  6846. ErrorSS(x.position,"could not import",filename);
  6847. IF VerboseErrorMessage THEN
  6848. Printout.Info("import",x)
  6849. END
  6850. ELSE
  6851. (*
  6852. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6853. (*! should rather be done by importer *)
  6854. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6855. checker.importCache := importCache;
  6856. checker.arrayBaseImported := arrayBaseImported;
  6857. checker.global := global;
  6858. checker.Module(module); (* semantic check *)
  6859. error := error OR checker.error;
  6860. END;
  6861. *)
  6862. (*
  6863. ASSERT(SyntaxTree.Resolved IN module.state);
  6864. *)
  6865. x.SetModule(module);
  6866. IF importCache # NIL THEN
  6867. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6868. import.SetContext(x.context);
  6869. import.SetModule(module);
  6870. importCache.AddImport(import);
  6871. END;
  6872. END;
  6873. ELSE
  6874. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6875. END;
  6876. END;
  6877. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6878. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6879. END;
  6880. import := module.moduleScope.firstImport;
  6881. WHILE(import # NIL) DO
  6882. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6883. ASSERT(currentScope # NIL);
  6884. ASSERT(currentScope.ownerModule # NIL);
  6885. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6886. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6887. Error(x.position,"recursive import");
  6888. ELSE
  6889. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6890. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6891. IF reimport = NIL THEN (* indirect import *)
  6892. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6893. reimport.SetContext(import.context);
  6894. reimport.SetModule(import.module);
  6895. moduleScope.AddImport(reimport);
  6896. reimport.SetScope(moduleScope);
  6897. ELSE
  6898. ASSERT(import.module # NIL);
  6899. reimport.SetModule(import.module); (* direct or indirect import *)
  6900. END;
  6901. END;
  6902. import := import.nextImport;
  6903. END;
  6904. END;
  6905. END;
  6906. currentScope := prevScope;
  6907. (* ELSE nothing to be done *)
  6908. x.SetState(SyntaxTree.Resolved);
  6909. END;
  6910. END VisitImport;
  6911. (*** statements ***)
  6912. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6913. VAR prev,resolved: SyntaxTree.Statement;
  6914. BEGIN
  6915. prev := resolvedStatement;
  6916. resolvedStatement := x;
  6917. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6918. activeCellsStatement := FALSE;
  6919. VStatement(x);
  6920. (* removed this, implementation restriction should be resolved by backend
  6921. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6922. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6923. END;
  6924. *)
  6925. resolved := resolvedStatement;
  6926. resolvedStatement := prev;
  6927. RETURN resolved
  6928. END ResolveStatement;
  6929. (** check and resolve statement sequence
  6930. - check all statements, replace if necessary
  6931. **)
  6932. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6933. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6934. BEGIN
  6935. IF statementSequence # NIL THEN (* else empty *)
  6936. FOR i := 0 TO statementSequence.Length()-1 DO
  6937. statement := statementSequence.GetStatement(i);
  6938. resolved := ResolveStatement(statement);
  6939. IF (resolved # statement) THEN
  6940. statementSequence.SetStatement(i,resolved);
  6941. END;
  6942. END;
  6943. END;
  6944. END StatementSequence;
  6945. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6946. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6947. - check if procedure is callable
  6948. - check return type = NIL (otherwise must be assignment statement)
  6949. **)
  6950. PROCEDURE VisitProcedureCallStatement*(procedureCall: SyntaxTree.ProcedureCallStatement);
  6951. VAR call: SyntaxTree.Designator;
  6952. BEGIN
  6953. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6954. call := procedureCall.call;
  6955. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6956. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6957. END;
  6958. call := ResolveDesignator(call);
  6959. IF call = SyntaxTree.invalidDesignator THEN
  6960. (* error already handled *)
  6961. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6962. (* inline call in a statement *)
  6963. ELSIF ~IsCallable(call) THEN
  6964. Error(procedureCall.position,"called object is not a procedure");
  6965. ELSIF (call.type # NIL) & (call.left # NIL) THEN
  6966. IF call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention THEN
  6967. Warning(procedureCall.position,"ignoring return value of procedure call");
  6968. ELSE
  6969. Error(procedureCall.position,"calling procedure with non-void return type");
  6970. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6971. END;
  6972. END;
  6973. procedureCall.SetCall(call);
  6974. (*
  6975. IF call = SyntaxTree.invalidDesignator THEN
  6976. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6977. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6978. IF IsOberonInline(procedure) THEN
  6979. Warning(procedure.position,"call to inline proc");
  6980. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6981. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6982. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6983. resolvedStatement := block;
  6984. RETURN;
  6985. END;
  6986. END;
  6987. *)
  6988. END VisitProcedureCallStatement;
  6989. (** check and resolve assignment LHS := RHS
  6990. - resolve LHS and RHS
  6991. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6992. - check if assignment is compatible
  6993. - check if LHS is variable (i.e. assignable)
  6994. - convert RHS if necessary
  6995. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6996. - assignment between different ASOTs
  6997. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6998. - assignment to ASOT elements:
  6999. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7000. **)
  7001. PROCEDURE VisitAssignment*(assignment: SyntaxTree.Assignment);
  7002. VAR
  7003. left: SyntaxTree.Designator;
  7004. right, expression: SyntaxTree.Expression;
  7005. designator: SyntaxTree.Designator;
  7006. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7007. mathArrayType: SyntaxTree.MathArrayType;
  7008. BEGIN
  7009. right := ResolveExpression(assignment.right);
  7010. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  7011. left := ResolveDesignator(assignment.left);
  7012. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7013. (* error already handled *)
  7014. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  7015. (* LHS is index write operator call on ASOT *)
  7016. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  7017. (* necessary ?
  7018. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  7019. type := procedureType.firstParameter.type;
  7020. expression := procedureCallDesignator.parameters.GetExpression(0);
  7021. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  7022. *)
  7023. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7024. ELSIF CheckVariable(left) THEN
  7025. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  7026. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7027. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7028. (* conversion done by procedure call
  7029. (* try to convert to left argument *)
  7030. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7031. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7032. procedureCallDesignator.parameters.SetExpression(1, right);
  7033. END;
  7034. *)
  7035. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  7036. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  7037. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  7038. ELSIF AssignmentCompatible(left, right) THEN
  7039. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  7040. mathArrayType := MathArrayStructureOfType(left.type);
  7041. right := NewConversion(right.position, right, mathArrayType, NIL);
  7042. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  7043. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  7044. ELSE
  7045. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7046. assignment.SetLeft(left);
  7047. assignment.SetRight(right);
  7048. resolvedStatement := assignment
  7049. END
  7050. END
  7051. END
  7052. END VisitAssignment;
  7053. (** check and resolve assignment LHS := RHS
  7054. - resolve LHS and RHS
  7055. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  7056. - check if assignment is compatible
  7057. - check if LHS is variable (i.e. assignable)
  7058. - convert RHS if necessary
  7059. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  7060. - assignment between different ASOTs
  7061. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  7062. - assignment to ASOT elements:
  7063. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  7064. **)
  7065. PROCEDURE VisitCommunicationStatement*(communication: SyntaxTree.CommunicationStatement);
  7066. VAR
  7067. left: SyntaxTree.Designator;
  7068. right: SyntaxTree.Expression;
  7069. inPort, outPort: SyntaxTree.PortType;
  7070. expression: SyntaxTree.Expression;
  7071. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  7072. BEGIN
  7073. right := ResolveExpression(communication.right);
  7074. left := ResolveDesignator(communication.left);
  7075. communication.SetLeft(left);
  7076. communication.SetRight(right);
  7077. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  7078. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  7079. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  7080. (* conversion done by procedure call
  7081. (* try to convert to left argument *)
  7082. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  7083. right := NewConversion(right.position, right, left.type.resolved, NIL);
  7084. procedureCallDesignator.parameters.SetExpression(1, right);
  7085. END;
  7086. *)
  7087. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  7088. ELSE
  7089. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  7090. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  7091. (* error already handled *)
  7092. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  7093. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  7094. IF outPort.direction # SyntaxTree.OutPort THEN
  7095. Error(left.position,"not an out-port")
  7096. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7097. Error(left.position,"incompatible to port type");
  7098. ELSE
  7099. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7100. communication.SetRight(right)
  7101. END;
  7102. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  7103. IF CheckVariable(left) THEN
  7104. IF inPort.direction # SyntaxTree.InPort THEN
  7105. Error(left.position,"not an in-port")
  7106. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  7107. Error(right.position,"incompatible to port type");
  7108. END;
  7109. END;
  7110. ELSE
  7111. Error(communication.position,"unsupported stream operation");
  7112. END;
  7113. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  7114. IF outPort.direction # SyntaxTree.OutPort THEN
  7115. Error(left.position,"not an out-port")
  7116. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  7117. Error(left.position,"incompatible to port type");
  7118. ELSE
  7119. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  7120. communication.SetRight(right)
  7121. END;
  7122. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  7123. IF CheckVariable(right) THEN
  7124. IF inPort.direction # SyntaxTree.InPort THEN
  7125. Error(left.position,"not an in-port")
  7126. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7127. Error(right.position,"incompatible to port type");
  7128. END;
  7129. END;
  7130. ELSE
  7131. Error(communication.position, "unsupported operation");
  7132. END;
  7133. END;
  7134. END VisitCommunicationStatement;
  7135. (** check and resolve if/eslif part
  7136. - check condition
  7137. - check statement sequence
  7138. **)
  7139. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7140. VAR prevUnreachable, b: BOOLEAN;
  7141. BEGIN
  7142. prevUnreachable := currentIsUnreachable;
  7143. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7144. IF IsBooleanValue(ifPart.condition,b) THEN
  7145. IF b=FALSE THEN
  7146. currentIsUnreachable := TRUE
  7147. ELSIF b=TRUE THEN
  7148. true := TRUE
  7149. END;
  7150. END;
  7151. StatementSequence(ifPart.statements);
  7152. currentIsUnreachable := prevUnreachable;
  7153. END IfPart;
  7154. (** check and resolve if statement
  7155. - check if parts and else part statement sequence
  7156. **)
  7157. PROCEDURE VisitIfStatement*(ifStatement: SyntaxTree.IfStatement);
  7158. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7159. BEGIN
  7160. prevUnreachable := currentIsUnreachable;
  7161. ifPartTrue := FALSE;
  7162. IfPart(ifStatement.ifPart,ifPartTrue);
  7163. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7164. elsif := ifStatement.GetElsifPart(i);
  7165. IfPart(elsif,ifPartTrue);
  7166. END;
  7167. IF ifStatement.elsePart # NIL THEN
  7168. IF ifPartTrue THEN
  7169. currentIsUnreachable := TRUE
  7170. END;
  7171. StatementSequence(ifStatement.elsePart)
  7172. END;
  7173. currentIsUnreachable := prevUnreachable;
  7174. END VisitIfStatement;
  7175. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; variable: SyntaxTree.Designator);
  7176. VAR
  7177. type,variableType: SyntaxTree.Type;
  7178. withEntry: WithEntry;
  7179. symbol: SyntaxTree.Symbol
  7180. BEGIN
  7181. type := ResolveType(withPart.type);
  7182. withPart.SetType(type);
  7183. variableType := variable.type.resolved;
  7184. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7185. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7186. END;
  7187. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7188. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7189. Error(variable.position,"is not extensible designator");
  7190. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7191. Error(variable.position,"is no local variable ");
  7192. IF VerboseErrorMessage THEN
  7193. Printout.Info("variable",variable)
  7194. END;
  7195. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7196. Error(variable.position,"withguarded symbol is no type extension of ");
  7197. IF VerboseErrorMessage THEN
  7198. Printout.Info("variable",variable);
  7199. Printout.Info("type",type);
  7200. END;
  7201. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7202. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7203. Error(variable.position,"withguarded symbol is no variable ");
  7204. IF VerboseErrorMessage THEN
  7205. Printout.Info("variable",variable);
  7206. Printout.Info("type",type);
  7207. END;
  7208. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7209. Error(variable.position,"invalid change of withguarded symbol");
  7210. ELSE
  7211. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7212. NEW(withEntry);
  7213. withEntry.previous := withEntries;
  7214. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7215. withEntry.type := type;
  7216. withEntries := withEntry;
  7217. StatementSequence(withPart.statements);
  7218. withEntries := withEntries.previous;
  7219. END;
  7220. END WithPart;
  7221. (** check and resolve with statement WITH variable: type DO ... END;
  7222. - check type and variable
  7223. - check that variable type is type extension of type
  7224. - check that variable is a variable
  7225. - enter new with scope and enter guardedVariable with same name and reference to variable
  7226. - create if statement:
  7227. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7228. **)
  7229. PROCEDURE VisitWithStatement*(withStatement: SyntaxTree.WithStatement);
  7230. VAR i,j: LONGINT; prevScope: SyntaxTree.Scope; variable: SyntaxTree.Designator;
  7231. BEGIN
  7232. prevScope := currentScope;
  7233. variable := ResolveDesignator(withStatement.variable);
  7234. withStatement.SetVariable(variable);
  7235. FOR i := 0 TO withStatement.WithParts()-1 DO
  7236. WithPart(withStatement.GetWithPart(i),variable);
  7237. END;
  7238. FOR i := 0 TO withStatement.WithParts()-1 DO
  7239. FOR j := i+1 TO withStatement.WithParts()-1 DO
  7240. IF IsTypeExtension(withStatement.GetWithPart(i).type, withStatement.GetWithPart(j).type) THEN
  7241. Error(withStatement.GetWithPart(j).position, "unreachable extended type");
  7242. END;
  7243. END;
  7244. END;
  7245. IF withStatement.elsePart # NIL THEN
  7246. StatementSequence(withStatement.elsePart)
  7247. END;
  7248. currentScope := prevScope;
  7249. END VisitWithStatement;
  7250. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7251. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7252. - check 'first' < 'last' and no overlaps between different case labels
  7253. - check statement sequence
  7254. **)
  7255. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: Basic.Integer);
  7256. VAR
  7257. i: LONGINT;
  7258. position: Position;
  7259. expression, left, right: SyntaxTree.Expression;
  7260. expressionType: SyntaxTree.Type;
  7261. l, r: Basic.Integer;
  7262. cl, cr: CHAR;
  7263. thiscases: SyntaxTree.CaseConstant;
  7264. BEGIN
  7265. thiscases := NIL;
  7266. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7267. expression := casePart.elements.GetExpression(i);
  7268. position := expression.position;
  7269. (* set context of range *)
  7270. IF expression IS SyntaxTree.RangeExpression THEN
  7271. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7272. END;
  7273. expression := ResolveExpression(expression);
  7274. IF expression = SyntaxTree.invalidExpression THEN
  7275. (* error already reported *)
  7276. expressionType := SyntaxTree.invalidType;
  7277. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7278. (* read out 'first' and 'last' *)
  7279. left := expression(SyntaxTree.RangeExpression).first;
  7280. right := expression(SyntaxTree.RangeExpression).last;
  7281. (* guaranteed by VisitRangeExpression: *)
  7282. ASSERT((left # NIL) & (right # NIL));
  7283. ASSERT(left.type.resolved = right.type.resolved);
  7284. left := CompatibleConversion(left.position, left, type);
  7285. right := CompatibleConversion(right.position, right, type);
  7286. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7287. expression(SyntaxTree.RangeExpression).SetLast(right);
  7288. expressionType := RegularType(position,left.type);
  7289. ELSE
  7290. expression := ConstantExpression(expression);
  7291. expression := CompatibleConversion(expression.position, expression, type);
  7292. (*
  7293. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7294. left := Global.NewCharacterValue(system,expression.position,cl);
  7295. expression := casePart.elements.GetExpression(i);
  7296. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7297. expression := left
  7298. END;
  7299. *)
  7300. casePart.elements.SetExpression(i,expression);
  7301. left := expression; right := expression;
  7302. expressionType := RegularType(position,expression.type)
  7303. END;
  7304. IF (expressionType = SyntaxTree.invalidType) THEN
  7305. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7306. Error(position, "inadmissible case label");
  7307. expression := SyntaxTree.invalidExpression;
  7308. ELSE
  7309. l := 0; r := 0;
  7310. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7311. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7312. l := ORD(cl); r := ORD(cr);
  7313. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7314. ELSE
  7315. expression := SyntaxTree.invalidExpression
  7316. END;
  7317. IF expression # SyntaxTree.invalidExpression THEN
  7318. IF l>r THEN
  7319. Error(position, "empty case label")
  7320. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7321. Error(position, "duplicate case label");
  7322. ELSE
  7323. IF l < min THEN min := l END;
  7324. IF r > max THEN max := r END;
  7325. END;
  7326. END;
  7327. END;
  7328. casePart.elements.SetExpression(i,expression);
  7329. END;
  7330. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7331. casePart.SetConstants(thiscases);
  7332. StatementSequence(casePart.statements);
  7333. END CasePart;
  7334. (** check and resolve case statement CASE variable OF ... END;
  7335. - check variable
  7336. - check case parts
  7337. **)
  7338. PROCEDURE VisitCaseStatement*(caseStatement: SyntaxTree.CaseStatement);
  7339. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7340. ch: CHAR; l: Basic.Integer; min,max: Basic.Integer;
  7341. BEGIN
  7342. expression := ResolveExpression(caseStatement.variable);
  7343. type := RegularType(expression.position,expression.type);
  7344. IF type = SyntaxTree.invalidType THEN
  7345. expression := SyntaxTree.invalidExpression;
  7346. ELSIF IsIntegerType(type) THEN
  7347. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7348. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7349. (*
  7350. expression := Global.NewCharacterValue(system,expression.position,ch);
  7351. *)
  7352. type := expression.type;
  7353. ELSIF IsCharacterType(type) THEN
  7354. ELSIF IsEnumerationType(type) THEN
  7355. ELSE
  7356. Error(caseStatement.variable.position,"variable must be integer or character type");
  7357. expression := SyntaxTree.invalidExpression;
  7358. END;
  7359. caseStatement.SetVariable(expression);
  7360. caseList := NIL;
  7361. min := MAX(Basic.Integer); max := MIN(Basic.Integer);
  7362. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7363. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7364. END;
  7365. caseStatement.SetMinMax(min,max);
  7366. StatementSequence(caseStatement.elsePart);
  7367. IF expression.resolved # NIL THEN
  7368. IF IsCharacterValue(expression,ch) THEN
  7369. l := ORD(ch)
  7370. ELSIF IsIntegerValue(expression,l) THEN
  7371. END;
  7372. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7373. END;
  7374. END VisitCaseStatement;
  7375. (** check and resolve while statement
  7376. - check condition
  7377. - check statement sequence
  7378. **)
  7379. PROCEDURE VisitWhileStatement*(whileStatement: SyntaxTree.WhileStatement);
  7380. VAR prevIsUnreachable,b: BOOLEAN;
  7381. BEGIN
  7382. prevIsUnreachable := currentIsUnreachable;
  7383. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7384. IF IsBooleanValue(whileStatement.condition,b) THEN
  7385. IF b=FALSE THEN
  7386. currentIsUnreachable := TRUE
  7387. END;
  7388. END;
  7389. StatementSequence(whileStatement.statements);
  7390. currentIsUnreachable := prevIsUnreachable
  7391. END VisitWhileStatement;
  7392. (** check and resolve repeat statement
  7393. - check condition
  7394. - check statement sequence
  7395. **)
  7396. PROCEDURE VisitRepeatStatement*(repeatStatement: SyntaxTree.RepeatStatement);
  7397. BEGIN
  7398. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7399. StatementSequence(repeatStatement.statements);
  7400. END VisitRepeatStatement;
  7401. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7402. VAR withEntry: WithEntry;
  7403. BEGIN
  7404. withEntry := withEntries;
  7405. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7406. withEntry := withEntry.previous
  7407. END;
  7408. IF withEntry = NIL THEN RETURN FALSE
  7409. ELSE
  7410. type := withEntry.type;
  7411. RETURN TRUE
  7412. END;
  7413. END GetGuard;
  7414. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7415. - check that variable is an integer variable
  7416. - check that from is integer typed with compatible type
  7417. - check that to has compatible type
  7418. - check that by is constant integer with compatible type
  7419. **)
  7420. PROCEDURE VisitForStatement*(forStatement: SyntaxTree.ForStatement);
  7421. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7422. BEGIN
  7423. designator := ResolveDesignator(forStatement.variable);
  7424. type := SyntaxTree.invalidType;
  7425. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7426. designator := SyntaxTree.invalidDesignator;
  7427. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7428. Error(designator.position,"control variable of non-integer type");
  7429. designator := SyntaxTree.invalidDesignator;
  7430. ELSIF CheckVariable(designator) THEN
  7431. type := designator.type;
  7432. END;
  7433. forStatement.SetVariable(designator);
  7434. expression := ResolveExpression(forStatement.from);
  7435. IF expression = SyntaxTree.invalidExpression THEN
  7436. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7437. Error(expression.position,"start value of incompatible type");
  7438. expression := SyntaxTree.invalidExpression;
  7439. ELSIF type # SyntaxTree.invalidType THEN
  7440. expression := NewConversion(expression.position,expression,type,NIL)
  7441. END;
  7442. forStatement.SetFrom(expression);
  7443. expression := ResolveExpression(forStatement.to);
  7444. IF expression = SyntaxTree.invalidExpression THEN
  7445. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7446. Error(expression.position,"end value of incompatible type");
  7447. expression := SyntaxTree.invalidExpression;
  7448. ELSIF type # SyntaxTree.invalidType THEN
  7449. expression := NewConversion(expression.position,expression,type,NIL)
  7450. END;
  7451. forStatement.SetTo(expression);
  7452. IF forStatement.by # NIL THEN
  7453. expression := ConstantInteger(forStatement.by);
  7454. ELSE
  7455. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7456. END;
  7457. IF expression = SyntaxTree.invalidExpression THEN
  7458. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7459. Error(expression.position,"step value of incompatible type");
  7460. ELSIF (expression.resolved(SyntaxTree.IntegerValue).value = 0) THEN
  7461. Error(expression.position,"invalid step value");
  7462. ELSIF type # SyntaxTree.invalidType THEN
  7463. expression := NewConversion(expression.position,expression,type,NIL)
  7464. END;
  7465. forStatement.SetBy(expression);
  7466. StatementSequence(forStatement.statements);
  7467. END VisitForStatement;
  7468. (** check and resolve loop statement LOOP StatementSequence END
  7469. - check statement sequence
  7470. **)
  7471. PROCEDURE VisitLoopStatement*(loopStatement: SyntaxTree.LoopStatement);
  7472. BEGIN
  7473. StatementSequence(loopStatement.statements)
  7474. END VisitLoopStatement;
  7475. PROCEDURE VisitExitableBlock*(exitableBlock: SyntaxTree.ExitableBlock);
  7476. BEGIN
  7477. StatementSequence(exitableBlock.statements);
  7478. END VisitExitableBlock;
  7479. (** check and resolve exit statement EXIT
  7480. - check that exit is within LOOP statement block
  7481. **)
  7482. PROCEDURE VisitExitStatement*(exitStatement: SyntaxTree.ExitStatement);
  7483. VAR outer: SyntaxTree.Statement;
  7484. BEGIN
  7485. outer := exitStatement.outer;
  7486. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7487. outer := outer.outer;
  7488. END;
  7489. IF outer = NIL THEN
  7490. Error(exitStatement.position,"exit statement not within loop statement");
  7491. END;
  7492. END VisitExitStatement;
  7493. (** check and resolve return statement RETURN [expression]
  7494. - check expression (if any)
  7495. - check if in procedure scope
  7496. - if in procedure scope then check expression compatibility
  7497. - if not in procecdure scope then check on return without expression
  7498. **)
  7499. PROCEDURE VisitReturnStatement*(returnStatement: SyntaxTree.ReturnStatement);
  7500. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7501. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7502. BEGIN
  7503. position := returnStatement.position;
  7504. expression := returnStatement.returnValue;
  7505. IF expression # NIL THEN
  7506. expression := ResolveExpression(expression);
  7507. returnStatement.SetReturnValue(expression);
  7508. END;
  7509. outer := returnStatement.outer;
  7510. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7511. outer := outer.outer
  7512. END;
  7513. IF (outer # NIL) THEN
  7514. scope := outer(SyntaxTree.Body).inScope;
  7515. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7516. IF (expression # NIL) THEN
  7517. Error(position, "return statement with parameter not in procedure scope");
  7518. END;
  7519. ELSE
  7520. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7521. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7522. Error(position, "return statement in procedure that does not return");
  7523. END;
  7524. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7525. IF returnType # NIL THEN
  7526. returnType := returnType.resolved;
  7527. IF expression = NIL THEN
  7528. Error(position, "empty return type in procedure providing a return type")
  7529. ELSIF expression.type = NIL THEN
  7530. Error(position,"returned type incompatible: expression has no type");
  7531. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7532. Error(position, "return type not compatible");
  7533. IF VerboseErrorMessage THEN
  7534. Printout.Info("returnType",returnType);
  7535. Printout.Info("expression",expression);
  7536. END;
  7537. ELSE
  7538. expression := NewConversion(expression.position,expression,returnType,NIL);
  7539. returnStatement.SetReturnValue(expression);
  7540. END;
  7541. ELSIF expression # NIL THEN
  7542. Error(position, "non-empty return type in procedure providing no return type");
  7543. END;
  7544. END;
  7545. END;
  7546. END VisitReturnStatement;
  7547. (** check and resolve await statement AWAIT(condition: Expression)
  7548. - check await condition
  7549. **)
  7550. PROCEDURE VisitAwaitStatement*(awaitStatement: SyntaxTree.AwaitStatement);
  7551. VAR condition: SyntaxTree.Expression;
  7552. BEGIN
  7553. condition := ResolveCondition(awaitStatement.condition);
  7554. IF currentIsRealtime THEN
  7555. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7556. END;
  7557. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7558. Error(awaitStatement.position,"senseless await statement with constant condition");
  7559. END;
  7560. awaitStatement.SetCondition(condition);
  7561. END VisitAwaitStatement;
  7562. PROCEDURE CheckSystemImport(position: Position);
  7563. VAR import: SyntaxTree.Import;
  7564. BEGIN
  7565. import := currentScope.ownerModule.moduleScope.firstImport;
  7566. WHILE(import # NIL) DO
  7567. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7568. RETURN;
  7569. END;
  7570. import := import.nextImport;
  7571. END;
  7572. Error(position, "forbidden code without system import");
  7573. END CheckSystemImport;
  7574. (** check and resolve code statement: do nothing, must be done by assembler
  7575. **)
  7576. PROCEDURE VisitCode*(code: SyntaxTree.Code);
  7577. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7578. BEGIN
  7579. CheckSystemImport(code.position);
  7580. FOR i := 0 TO code.inRules.Length()-1 DO
  7581. statement := code.inRules.GetStatement(i);
  7582. IF statement IS SyntaxTree.Assignment THEN
  7583. WITH statement: SyntaxTree.Assignment DO
  7584. statement.SetRight(ResolveExpression(statement.right));
  7585. END;
  7586. ELSE
  7587. Error(statement.position, "can only be assignment")
  7588. END;
  7589. END;
  7590. FOR i := 0 TO code.outRules.Length()-1 DO
  7591. statement := code.outRules.GetStatement(i);
  7592. IF statement IS SyntaxTree.Assignment THEN
  7593. WITH statement: SyntaxTree.Assignment DO
  7594. statement.SetLeft(ResolveDesignator(statement.left));
  7595. END;
  7596. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7597. (* must be a reference to some register *)
  7598. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7599. ELSE
  7600. Printout.Info("out statement ", statement);
  7601. Error(statement.position, "(out) can only be assignment")
  7602. END;
  7603. END;
  7604. END VisitCode;
  7605. (** check and set flags of a statement block
  7606. - check for multiply occurence of a flag
  7607. - check and set priority only in bodies
  7608. - check for valid names
  7609. **)
  7610. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7611. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7612. flag: LONGINT; recordBody: SyntaxTree.Body;
  7613. PROCEDURE SetProtectedRecord;
  7614. VAR scope: SyntaxTree.Scope;
  7615. BEGIN
  7616. scope := currentScope;
  7617. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7618. scope := scope.outerScope
  7619. END;
  7620. IF scope # NIL THEN
  7621. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7622. END;
  7623. END SetProtectedRecord;
  7624. BEGIN
  7625. flags := {};
  7626. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7627. recordBody := block(SyntaxTree.Body)
  7628. ELSE
  7629. recordBody := NIL
  7630. END;
  7631. blockModifier := block.blockModifiers;
  7632. WHILE(blockModifier # NIL) DO
  7633. name := blockModifier.identifier;
  7634. expression := blockModifier.expression;
  7635. position := blockModifier.position;
  7636. flag := -1;
  7637. IF name=Global.NamePriority THEN
  7638. IF expression = NIL THEN
  7639. Error(position, "missing priority expression");
  7640. ELSIF recordBody = NIL THEN
  7641. Error(position, "priority not on record body");
  7642. ELSIF recordBody.priority # NIL THEN
  7643. Error(position, "duplicate priority expression");
  7644. ELSE
  7645. recordBody.SetPriority(expression);
  7646. END;
  7647. ELSIF expression # NIL THEN
  7648. Error(expression.position,"expression not in connection with priority")
  7649. ELSIF name=Global.NameExclusive THEN
  7650. IF block.isExclusive THEN
  7651. Error(position, "duplicate exclusive flag")
  7652. END;
  7653. block.SetExclusive(TRUE); SetProtectedRecord;
  7654. ELSIF name=Global.NameActive THEN
  7655. IF recordBody = NIL THEN
  7656. Error(position, "active not in record body");
  7657. ELSIF recordBody.isActive THEN
  7658. Error(position, "duplicate active flag")
  7659. ELSE
  7660. recordBody.SetActive(TRUE); SetProtectedRecord;
  7661. END;
  7662. ELSIF name=Global.NameSafe THEN
  7663. IF recordBody = NIL THEN
  7664. Error(position, "safe not in record body");
  7665. ELSIF recordBody.isSafe THEN
  7666. Error(position, "duplicate safe flag")
  7667. ELSE
  7668. recordBody.SetSafe(TRUE);
  7669. SetProtectedRecord;
  7670. END;
  7671. ELSIF name=Global.NameRealtime THEN
  7672. IF recordBody = NIL THEN
  7673. Error(position, "realtime not in record body");
  7674. ELSIF recordBody.isRealtime THEN
  7675. Error(position, "duplicate realtime flag")
  7676. ELSE
  7677. recordBody.SetRealtime(TRUE);
  7678. block.SetRealtime(TRUE);
  7679. END;
  7680. ELSIF name=Global.NameUnchecked THEN
  7681. IF block.isUnchecked THEN
  7682. Error(position, "duplicate unchecked flag")
  7683. ELSE
  7684. block.SetUnchecked(TRUE);
  7685. END;
  7686. ELSIF (name=Global.NameUncooperative) THEN
  7687. IF block.isUncooperative THEN
  7688. Error(position, "duplicate uncooperative flag")
  7689. ELSE
  7690. block.SetUncooperative(TRUE);
  7691. END;
  7692. ELSE
  7693. Error(position, "unknown block modifier");
  7694. END;
  7695. blockModifier := blockModifier.nextModifier;
  7696. END;
  7697. END BlockFlags;
  7698. (** check and resolve statement block
  7699. - check flags (exclusive)
  7700. - check statement sequence
  7701. **)
  7702. PROCEDURE VisitStatementBlock*(statementBlock: SyntaxTree.StatementBlock);
  7703. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7704. BEGIN
  7705. BlockFlags(statementBlock);
  7706. IF statementBlock.isExclusive THEN
  7707. (* check that not in exclusive block *)
  7708. IF currentIsExclusive THEN
  7709. Error (statementBlock.position,"forbidden recursive exclusive")
  7710. ELSIF currentIsRealtime THEN
  7711. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7712. END;
  7713. END;
  7714. recentExclusive := currentIsExclusive;
  7715. recentUnreachable := currentIsUnreachable;
  7716. recentRealtime := currentIsRealtime;
  7717. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7718. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7719. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7720. StatementSequence(statementBlock.statements);
  7721. currentIsRealtime := recentRealtime;
  7722. currentIsExclusive := recentExclusive;
  7723. currentIsUnreachable := recentUnreachable;
  7724. END VisitStatementBlock;
  7725. (** check and resolve body
  7726. - check flags (active, priority, safe)
  7727. - check body and finally part
  7728. **)
  7729. PROCEDURE Body(body: SyntaxTree.Body);
  7730. BEGIN
  7731. VisitStatementBlock(body);
  7732. IF body.isActive THEN
  7733. IF ~currentIsBodyProcedure THEN
  7734. Error(body.position,"active flag not in object body");
  7735. ELSIF body.priority # NIL THEN
  7736. body.SetPriority(ConstantInteger(body.priority));
  7737. END;
  7738. ELSIF body.isSafe THEN
  7739. Error(body.position,"safe flag not in active body");
  7740. ELSIF body.priority # NIL THEN
  7741. Error(body.position,"priority flag not in active body");
  7742. END;
  7743. IF body.code # NIL THEN
  7744. CheckSystemImport(body.position);
  7745. END;
  7746. StatementSequence(body.finally)
  7747. END Body;
  7748. (*** scopes ***)
  7749. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7750. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7751. VAR duplicateSymbol: BOOLEAN;
  7752. BEGIN
  7753. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7754. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7755. Error(symbol.position,"globally defined keyword")
  7756. END;
  7757. scope.EnterSymbol(symbol,duplicateSymbol);
  7758. IF ~allowDuplicate & duplicateSymbol THEN
  7759. Error(symbol.position,"Multiply defined identifier.");
  7760. IF VerboseErrorMessage THEN
  7761. Printout.Info("multiply defined identifier",symbol);
  7762. Printout.Info("in scope",scope);
  7763. END;
  7764. END;
  7765. END Register;
  7766. (**
  7767. implementation: check and resolve an implementation part
  7768. **)
  7769. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7770. move implementation checker to a separate object ? *)
  7771. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7772. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7773. BEGIN
  7774. prevIsRealtime := currentIsRealtime;
  7775. prevIsBodyProcedure := currentIsBodyProcedure;
  7776. prevIsCellNet := currentIsCellNet;
  7777. prevScope := currentScope;
  7778. currentScope := scope;
  7779. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7780. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7781. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7782. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7783. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7784. (*
  7785. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7786. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7787. END;
  7788. *)
  7789. END;
  7790. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7791. (* module body, record bodies are wrapped into an artifical procedure *)
  7792. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7793. Body(scope(SyntaxTree.ProcedureScope).body)
  7794. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7795. Body(scope(SyntaxTree.ProcedureScope).body)
  7796. END;
  7797. END;
  7798. currentScope := prevScope;
  7799. currentIsRealtime := prevIsRealtime;
  7800. currentIsBodyProcedure := prevIsBodyProcedure;
  7801. currentIsCellNet := prevIsCellNet;
  7802. END Implementation;
  7803. (** implementation phase:
  7804. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7805. **)
  7806. PROCEDURE Implementations(x: SyntaxTree.Module);
  7807. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7808. BEGIN
  7809. prevPhase := phase;
  7810. phase := InlinePhase;
  7811. scope := x.firstScope;
  7812. WHILE(scope # NIL) DO
  7813. Implementation(scope);
  7814. scope := scope.nextScope;
  7815. END;
  7816. phase := ImplementationPhase;
  7817. scope := x.firstScope;
  7818. WHILE(scope # NIL) DO
  7819. Implementation(scope);
  7820. scope := scope.nextScope;
  7821. END;
  7822. phase := prevPhase;
  7823. END Implementations;
  7824. (** declaration phase:
  7825. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7826. - import lists (for module scopes)
  7827. - parameter list (for procedure scopes)
  7828. - constant declarations
  7829. - type declarations
  7830. - variable declarations
  7831. - procedure declarations
  7832. preformed in two stages:
  7833. - first all symbols are entered into the symbol table (with uniqueness check),
  7834. - then all symbols are resolved
  7835. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7836. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7837. phases :
  7838. 0 = before procedures
  7839. 1 = procedures and later
  7840. **)
  7841. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; phases: SET);
  7842. VAR
  7843. constant: SyntaxTree.Constant;
  7844. typeDeclaration: SyntaxTree.TypeDeclaration;
  7845. declaredType: SyntaxTree.Type;
  7846. variable: SyntaxTree.Variable;
  7847. procedure: SyntaxTree.Procedure;
  7848. procedureType : SyntaxTree.ProcedureType;
  7849. prevScope: SyntaxTree.Scope;
  7850. parameter: SyntaxTree.Parameter;
  7851. import: SyntaxTree.Import;
  7852. symbol: SyntaxTree.Symbol;
  7853. prevPhase: LONGINT;
  7854. prevError : BOOLEAN;
  7855. i: LONGINT;
  7856. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7857. VAR baseType: SyntaxTree.Type; property: SyntaxTree.Property;
  7858. BEGIN
  7859. IF type.baseType # NIL THEN
  7860. baseType := type.baseType.resolved;
  7861. IF baseType IS SyntaxTree.PointerType THEN
  7862. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7863. END;
  7864. (*
  7865. IF baseType IS SyntaxTree.CellType THEN
  7866. DeclareCell(baseType(SyntaxTree.CellType));
  7867. END;
  7868. *)
  7869. END;
  7870. parameter := type.firstParameter;
  7871. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7872. (*
  7873. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7874. variable.SetType(parameter.type);
  7875. variable.SetAccess(SyntaxTree.Hidden);
  7876. variable.SetModifiers(parameter.modifiers);
  7877. currentScope.PushVariable(variable);
  7878. *)
  7879. Register(parameter,scope, FALSE);
  7880. parameter := parameter.nextParameter;
  7881. END;
  7882. property := type.firstProperty;
  7883. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7884. (*
  7885. variable := currentScope.FindVariable(property.name);
  7886. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7887. prop := variable(SyntaxTree.Property);
  7888. ELSE (* add, duplicate symbols detection later *)
  7889. prop := SyntaxTree.NewProperty(property.position, property.name);
  7890. currentScope.PushVariable(prop);
  7891. END;
  7892. prop.SetType(property.type);
  7893. prop.SetValue(property.value);
  7894. prop.SetAccess(SyntaxTree.Hidden);
  7895. *)
  7896. Register(property, scope, FALSE);
  7897. property := property.nextProperty;
  7898. END;
  7899. END DeclareCell;
  7900. BEGIN
  7901. prevError := error;
  7902. prevPhase := phase;
  7903. phase := DeclarationPhase;
  7904. prevScope := currentScope;
  7905. currentScope := scope;
  7906. error := FALSE;
  7907. IF 0 IN phases THEN
  7908. (* first enter all symbols in scope *)
  7909. IF scope IS SyntaxTree.ModuleScope THEN
  7910. (* treat imports first for a module scope, , set default context if necessary *)
  7911. import := scope(SyntaxTree.ModuleScope).firstImport;
  7912. WHILE(import # NIL) DO
  7913. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7914. Register(import, currentScope, FALSE);
  7915. import := import.nextImport;
  7916. END;
  7917. import := scope(SyntaxTree.ModuleScope).firstImport;
  7918. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7919. ResolveSymbol(import);
  7920. import := import.nextImport;
  7921. END;
  7922. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7923. (* enter parameters for a procedure scope *)
  7924. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7925. parameter := procedureType.firstParameter;
  7926. WHILE(parameter # NIL) DO
  7927. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7928. END;
  7929. parameter := procedureType.returnParameter;
  7930. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7931. parameter := procedureType.selfParameter;
  7932. IF parameter # NIL THEN
  7933. Register(parameter, currentScope, FALSE);
  7934. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7935. END;
  7936. ELSIF scope IS SyntaxTree.CellScope THEN
  7937. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7938. IF~skipImplementation THEN
  7939. import := scope(SyntaxTree.CellScope).firstImport;
  7940. WHILE(import # NIL) DO
  7941. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7942. Register(import, currentScope, FALSE);
  7943. import := import.nextImport;
  7944. END;
  7945. import := scope(SyntaxTree.CellScope).firstImport;
  7946. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7947. ResolveSymbol(import);
  7948. import := import.nextImport;
  7949. END;
  7950. END;
  7951. END;
  7952. IF error THEN RETURN END;
  7953. IF skipImplementation THEN
  7954. scope.Clear;
  7955. END;
  7956. (* constants *)
  7957. constant := scope.firstConstant;
  7958. WHILE (constant # NIL) DO
  7959. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7960. END;
  7961. (* type declarations *)
  7962. typeDeclaration := scope.firstTypeDeclaration;
  7963. WHILE (typeDeclaration # NIL) DO
  7964. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7965. END;
  7966. (* variables *)
  7967. variable := scope.firstVariable;
  7968. WHILE (variable # NIL) DO
  7969. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7970. END;
  7971. (* procedures *)
  7972. IF scope.procedures # NIL THEN
  7973. FOR i := 0 TO scope.procedures.Length()-1 DO
  7974. procedure := scope.procedures.GetProcedure(i);
  7975. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7976. IF procedureType.selfParameter = NIL THEN
  7977. scope.AddProcedure(procedure);
  7978. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7979. ELSE
  7980. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7981. IF typeDeclaration = NIL THEN
  7982. Error(procedureType.selfParameter.position, "No such type declaration");
  7983. ELSE
  7984. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7985. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7986. declaredType := typeDeclaration.declaredType;
  7987. IF declaredType IS SyntaxTree.PointerType THEN
  7988. declaredType := declaredType(SyntaxTree.PointerType).pointerBase.resolved
  7989. END;
  7990. IF declaredType IS SyntaxTree.RecordType THEN
  7991. declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7992. Register(procedure, declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7993. ELSE
  7994. Error(procedureType.selfParameter.position,"type is no record or pointer to record");
  7995. END;
  7996. END;
  7997. END;
  7998. END;
  7999. END;
  8000. END;
  8001. (* now process all symbols without any presumption on the order *)
  8002. symbol := scope.firstSymbol;
  8003. WHILE(symbol # NIL) DO
  8004. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  8005. IF (symbol IS SyntaxTree.Procedure) THEN
  8006. IF 1 IN phases THEN
  8007. ResolveSymbol(symbol);
  8008. END;
  8009. ELSE
  8010. IF 0 IN phases THEN
  8011. ResolveSymbol(symbol);
  8012. END;
  8013. END;
  8014. END;
  8015. symbol := symbol.nextSymbol;
  8016. END;
  8017. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  8018. symbol := scope.firstSymbol;
  8019. WHILE symbol # NIL DO
  8020. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  8021. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  8022. pointerFixes.Add(symbol, currentScope);
  8023. END;
  8024. IF ~symbol.type.resolved.isRealtime THEN
  8025. Error(symbol.position,"symbol has no realtime type");
  8026. END;
  8027. END;
  8028. symbol := symbol.nextSymbol
  8029. END;
  8030. END;
  8031. IF ~error & (1 IN phases) & ~system.GenerateVariableOffsets(scope) THEN
  8032. Error(Basic.invalidPosition,"problems during offset computation in module");
  8033. END;
  8034. IF (scope.ownerModule # NIL) & (1 IN phases) THEN
  8035. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  8036. scope.ownerModule.AddScope(scope);
  8037. END;
  8038. phase := prevPhase;
  8039. currentScope := prevScope;
  8040. error := error OR prevError;
  8041. END Declarations;
  8042. (* nopov *)
  8043. (** check if all operators from one module are compatible to the ones in the other module
  8044. - check if there are not multiple operators with the same signature
  8045. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  8046. - check for all operators whose signatures are compatible, whether the return types are compatible
  8047. note that:
  8048. - the return type is not considered to be part of the signature
  8049. - two signatures are considered compatible, if all of the operands are compatible
  8050. **)
  8051. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  8052. VAR
  8053. thisOperator, thatOperator: SyntaxTree.Operator;
  8054. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  8055. thisParameter, thatParameter: SyntaxTree.Parameter;
  8056. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  8057. i: LONGINT;
  8058. BEGIN
  8059. currentScope := thisModuleScope;
  8060. hasError := FALSE;
  8061. (* go through all operators in the other module *)
  8062. thatOperator := thatModuleScope.firstOperator;
  8063. WHILE (thatOperator # NIL) & ~hasError DO
  8064. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  8065. (* the other operator is accessible *)
  8066. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  8067. (* the other operator is not the conversion operator *)
  8068. (* go through all operators in this module *)
  8069. thisOperator := thisModuleScope.firstOperator;
  8070. WHILE (thisOperator # NIL) & ~hasError DO
  8071. IF thisOperator # thatOperator THEN
  8072. (* the operators are not the same *)
  8073. IF thisOperator.name = thatOperator.name THEN
  8074. (* the operators share the same identifier *)
  8075. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  8076. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  8077. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  8078. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  8079. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  8080. (* both operators have the same paramter count *)
  8081. thisParameter := thisProcedureType.firstParameter;
  8082. thatParameter := thatProcedureType.firstParameter;
  8083. operandsAreEqual := TRUE;
  8084. operandsAreCompatible := TRUE;
  8085. (* go through all parameters *)
  8086. FOR i := 1 TO thisProcedureType.numberParameters DO
  8087. ASSERT(thatParameter # NIL);
  8088. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  8089. operandsAreEqual := FALSE;
  8090. IF TypeDistance(system, thisParameter.type, thatParameter.type, thisParameter.kind = SyntaxTree.VarParameter) =Infinity THEN
  8091. operandsAreCompatible := FALSE
  8092. END
  8093. END;
  8094. thisParameter := thisParameter.nextParameter;
  8095. thatParameter := thatParameter.nextParameter
  8096. END;
  8097. IF operandsAreEqual THEN
  8098. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  8099. hasError := TRUE
  8100. ELSIF operandsAreCompatible THEN
  8101. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  8102. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  8103. hasError := TRUE
  8104. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  8105. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  8106. hasError := TRUE
  8107. END
  8108. END
  8109. END
  8110. END
  8111. END;
  8112. thisOperator := thisOperator.nextOperator
  8113. END
  8114. END
  8115. END;
  8116. thatOperator := thatOperator.nextOperator
  8117. END
  8118. END CheckInterOperatorConformity;
  8119. (** check module:
  8120. - check module declaration
  8121. - add context, if necessary
  8122. - remove module from import cache, if necessary
  8123. - check declarations
  8124. - resolve all type fixes
  8125. - check implementation (bodies)
  8126. **)
  8127. PROCEDURE Module*(x: SyntaxTree.Module);
  8128. VAR (* nopov *)
  8129. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: Basic.Integer; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8130. BEGIN
  8131. prevScope := currentScope;
  8132. prevIsCellNet := currentIsCellNet;
  8133. module := x;
  8134. ASSERT(x # NIL);
  8135. global := system.globalScope[x.case];
  8136. x.moduleScope.SetGlobalScope(global);
  8137. currentScope := global;
  8138. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8139. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8140. RemoveModuleFromCache(importCache,x);
  8141. Declarations(x.moduleScope, FALSE, {0,1});
  8142. FixTypes();
  8143. IF module.isCellNet THEN
  8144. currentIsCellNet := TRUE;
  8145. modifier := x.modifiers;
  8146. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8147. CheckModifiers(modifier, FALSE);
  8148. END;
  8149. (* nopov *)
  8150. IF ~error THEN
  8151. (* check if operators conform to each other within this module *)
  8152. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8153. (* go through all imports *)
  8154. import := x.moduleScope.firstImport;
  8155. WHILE import # NIL DO
  8156. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8157. (* check if all operators in this module conform to the ones of the imported module *)
  8158. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8159. END;
  8160. import := import.nextImport
  8161. END;
  8162. END;
  8163. Implementations(x);
  8164. module := NIL;
  8165. currentIsCellNet := prevIsCellNet;
  8166. currentScope := prevScope;
  8167. END Module;
  8168. END Checker;
  8169. Warnings*=OBJECT (SyntaxTree.Visitor)
  8170. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8171. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8172. BEGIN
  8173. SELF.diagnostics := diagnostics
  8174. END InitWarnings;
  8175. (** types *)
  8176. PROCEDURE Type(CONST x: SyntaxTree.Type);
  8177. BEGIN
  8178. IF SyntaxTree.Warned IN x.state THEN RETURN END;
  8179. x.SetState(SyntaxTree.Warned);
  8180. WITH x:
  8181. SyntaxTree.ArrayType DO Type(x.arrayBase);
  8182. |SyntaxTree.MathArrayType DO Type(x.arrayBase);
  8183. |SyntaxTree.PointerType DO Type(x.pointerBase);
  8184. |SyntaxTree.RecordType DO Scope(x.recordScope);
  8185. |SyntaxTree.CellType DO Scope(x.cellScope)
  8186. ELSE
  8187. END;
  8188. END Type;
  8189. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8190. VAR msg: ARRAY 256 OF CHAR;
  8191. BEGIN
  8192. Global.GetSymbolName(x,msg);
  8193. Strings.Append(msg," ");
  8194. Strings.Append(msg,text);
  8195. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8196. END Warning;
  8197. (** symbols *)
  8198. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8199. BEGIN
  8200. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8201. IF ~(x IS SyntaxTree.Parameter) THEN
  8202. Warning(x,"never used");
  8203. END;
  8204. END;
  8205. WITH x:
  8206. SyntaxTree.Procedure DO
  8207. Scope(x.procedureScope)
  8208. ELSE
  8209. END;
  8210. END Symbol;
  8211. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8212. VAR
  8213. symbol: SyntaxTree.Symbol;
  8214. BEGIN
  8215. symbol := scope.firstSymbol;
  8216. WHILE(symbol # NIL) DO
  8217. Symbol(symbol);
  8218. symbol := symbol.nextSymbol;
  8219. END;
  8220. END Scope;
  8221. PROCEDURE Module*(x: SyntaxTree.Module);
  8222. BEGIN
  8223. SELF.module := x;
  8224. Scope(x.moduleScope);
  8225. END Module;
  8226. END Warnings;
  8227. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8228. BEGIN
  8229. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8230. END IsOberonInline;
  8231. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8232. BEGIN
  8233. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8234. END Resolved;
  8235. PROCEDURE PowerOf2(x: Basic.Integer): BOOLEAN;
  8236. VAR i: LONGINT;
  8237. BEGIN
  8238. i := 1;
  8239. WHILE i < x DO
  8240. i := i *2
  8241. END;
  8242. RETURN i=x
  8243. END PowerOf2;
  8244. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8245. BEGIN
  8246. RETURN
  8247. (scope # NIL) &
  8248. (scope IS SyntaxTree.ModuleScope)
  8249. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8250. OR
  8251. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8252. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8253. END IsCellNetScope;
  8254. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8255. BEGIN
  8256. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8257. END IsCellScope;
  8258. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8259. BEGIN
  8260. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8261. RETURN (scope # NIL) & IsCellNetScope(scope)
  8262. END InCellNetScope;
  8263. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8264. BEGIN
  8265. ASSERT(size MOD system.dataUnit = 0);
  8266. RETURN size DIV system.dataUnit
  8267. END ToMemoryUnits;
  8268. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8269. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8270. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8271. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8272. BEGIN
  8273. IF t = NIL THEN
  8274. RETURN TRUE
  8275. ELSE
  8276. t := t.resolved;
  8277. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8278. END;
  8279. END TypeAllowed;
  8280. BEGIN
  8281. type := type.resolved;
  8282. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8283. RETURN FALSE
  8284. ELSE
  8285. procedureType := type(SyntaxTree.ProcedureType);
  8286. numberParameters := procedureType.numberParameters;
  8287. RETURN
  8288. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8289. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8290. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8291. END;
  8292. END GetProcedureAllowed;
  8293. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8294. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8295. VAR import: SyntaxTree.Import;
  8296. BEGIN
  8297. import := importCache.ImportByModuleName(x.name,x.context);
  8298. IF import # NIL THEN
  8299. importCache.RemoveImporters(x.name,x.context);
  8300. END;
  8301. END RemoveModuleFromCache;
  8302. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8303. (* to <- this assignment compatibility *)
  8304. VAR result: BOOLEAN;
  8305. BEGIN
  8306. IF this= NIL THEN result := (to=NIL)
  8307. ELSIF to=NIL THEN result := FALSE
  8308. ELSE
  8309. (*! will be replaced by this:
  8310. ELSE result := this.CompatibleTo(to.resolved);
  8311. *)
  8312. this := this.resolved; to := to.resolved;
  8313. IF to=SyntaxTree.invalidType THEN result := FALSE
  8314. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8315. ELSIF (to = this) OR (to.SameType(this)) THEN
  8316. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8317. ELSIF to IS SyntaxTree.BasicType THEN
  8318. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8319. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8320. result := this.CompatibleTo(to.resolved)
  8321. ELSE
  8322. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8323. END
  8324. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8325. result := to.sizeInBits >= this.sizeInBits;
  8326. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8327. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8328. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8329. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8330. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8331. result := TRUE;
  8332. ELSIF to IS SyntaxTree.AnyType THEN
  8333. 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);
  8334. ELSIF to IS SyntaxTree.ObjectType THEN
  8335. 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 *) ;
  8336. ELSIF to IS SyntaxTree.ByteType THEN
  8337. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8338. ELSIF to IS SyntaxTree.CharacterType THEN
  8339. result := IsCharacterType(this)
  8340. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR IsAddressType(this, system.addressSize)) THEN
  8341. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8342. 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)
  8343. OR IsTensor(this)
  8344. ) THEN
  8345. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8346. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8347. result := TRUE;
  8348. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8349. result := TRUE;
  8350. ELSE
  8351. result := FALSE
  8352. END;
  8353. ELSIF IsUnsafePointer(to) & IsUnsafePointer(this) THEN
  8354. result := TRUE;
  8355. ELSIF to IS SyntaxTree.PointerType THEN
  8356. result := (this IS SyntaxTree.NilType) OR
  8357. IsUnsafePointer(to) & ( (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR IsPointerType(this) OR IsTensor(this)) OR
  8358. (IsPointerType(this) & IsTypeExtension(to,this) OR
  8359. ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this))) & (~to.isRealtime OR this.isRealtime);
  8360. ELSIF to IS SyntaxTree.ProcedureType THEN
  8361. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & this.CompatibleTo(to)
  8362. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8363. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8364. ELSIF to IS SyntaxTree.RecordType THEN
  8365. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8366. ELSIF to IS SyntaxTree.ArrayType THEN
  8367. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8368. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8369. ELSIF StaticArrayCompatible(to, this) THEN
  8370. result := TRUE
  8371. ELSE
  8372. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8373. END;
  8374. ELSIF to IS SyntaxTree.MathArrayType THEN
  8375. IF this IS SyntaxTree.MathArrayType THEN
  8376. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8377. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8378. result := TRUE;
  8379. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8380. result := TRUE;
  8381. ELSE
  8382. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8383. END;
  8384. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8385. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8386. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8387. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8388. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8389. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8390. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8391. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8392. ELSE
  8393. result := FALSE
  8394. END;
  8395. (* an array-structured object type is compatible to the type of its array structure *)
  8396. ELSIF IsArrayStructuredObjectType(this) THEN
  8397. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8398. ELSE
  8399. result := FALSE;
  8400. END;
  8401. ELSIF to IS SyntaxTree.StringType THEN
  8402. result := FALSE;
  8403. ELSIF to IS SyntaxTree.EnumerationType THEN
  8404. result := IsEnumerationExtension(this,to);
  8405. ELSIF to IS SyntaxTree.PortType THEN
  8406. result := SameType(to, this)
  8407. ELSE
  8408. Printout.Info("CompatibleTo",to);
  8409. HALT(100); (* implement missing type check *)
  8410. END;
  8411. END;
  8412. RETURN result
  8413. END CompatibleTo;
  8414. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8415. VAR actualBase, formalBase: SyntaxTree.Type;
  8416. BEGIN
  8417. IF SameType(formal,actual) THEN
  8418. RETURN TRUE
  8419. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8420. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8421. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8422. RETURN
  8423. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8424. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8425. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8426. & StaticArrayCompatible(formalBase,actualBase)
  8427. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8428. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8429. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8430. RETURN
  8431. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8432. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8433. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8434. & StaticArrayCompatible(formalBase,actualBase)
  8435. ELSE RETURN FALSE
  8436. END;
  8437. END StaticArrayCompatible;
  8438. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8439. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8440. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8441. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8442. BEGIN
  8443. result := SameType(formal,actual);
  8444. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8445. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8446. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8447. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8448. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8449. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8450. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8451. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8452. & TC(formalBase, actualBase);
  8453. END;
  8454. RETURN result
  8455. END TC;
  8456. BEGIN
  8457. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8458. ELSE
  8459. arrayBase := formalType.arrayBase.resolved;
  8460. IF (actualType IS SyntaxTree.StringType) THEN
  8461. result := arrayBase IS SyntaxTree.CharacterType
  8462. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8463. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8464. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8465. result := TC(formalType, actualType);
  8466. ELSE
  8467. result := (arrayBase IS SyntaxTree.ByteType)
  8468. END;
  8469. END;
  8470. RETURN result
  8471. END OpenArrayCompatible;
  8472. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8473. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8474. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8475. BEGIN
  8476. IF actualType IS SyntaxTree.MathArrayType THEN
  8477. actualArray := actualType(SyntaxTree.MathArrayType);
  8478. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8479. (*
  8480. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8481. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8482. *)
  8483. actualBase := ArrayBase(actualType,Infinity);
  8484. formalBase := ArrayBase(formalType,Infinity);
  8485. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8486. ELSE
  8487. (*
  8488. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8489. *)
  8490. formalBase := Resolved(formalType.arrayBase);
  8491. actualBase := Resolved(actualArray.arrayBase);
  8492. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8493. (*
  8494. ARRAY [k] -> ARRAY [n]
  8495. *)
  8496. result := (formalType.staticLength = actualArray.staticLength)
  8497. ELSE
  8498. result := TRUE
  8499. END;
  8500. IF ~result THEN
  8501. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8502. ELSIF actualBase = NIL THEN result := FALSE
  8503. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8504. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8505. ELSE
  8506. result := SameType(formalBase,actualBase)
  8507. END;
  8508. END;
  8509. ELSE
  8510. result := FALSE
  8511. END;
  8512. RETURN result
  8513. END MathArrayCompatible;
  8514. (**
  8515. Math Array Type distance for assignments / parameter passings of the form
  8516. from -> to
  8517. variants:
  8518. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8519. allowed:
  8520. static -> static (& size match)
  8521. static -> open
  8522. static -> tensor
  8523. open -> open
  8524. open -> tensor
  8525. open -> static
  8526. tensor -> tensor
  8527. tensor -> open
  8528. tensor -> static
  8529. **)
  8530. (*! think about the metric here: is form matching more important than element type matching? *)
  8531. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8532. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8533. BEGIN
  8534. fromBase := Resolved(from.arrayBase);
  8535. toBase := Resolved(to.arrayBase);
  8536. i := Infinity;
  8537. IF (from = to) OR (from.SameType(to)) THEN
  8538. i := 0;
  8539. ELSIF (from.form = to.form) THEN
  8540. (* static -> static, open -> open, tensor -> tensor *)
  8541. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8542. IF fromBase = toBase THEN i := 0
  8543. ELSIF toBase = NIL THEN i := 1
  8544. ELSIF fromBase = NIL THEN i := Infinity;
  8545. ELSIF toBase.SameType(fromBase) THEN i := 0
  8546. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8547. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8548. ELSE
  8549. i := TypeDistance(system,fromBase, toBase, varpar);
  8550. IF i < Infinity THEN i := i * 5 END;
  8551. END;
  8552. END;
  8553. ELSIF (to.form = SyntaxTree.Static) THEN
  8554. (* forbidden *)
  8555. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8556. (* static -> tensor, open -> tensor, tensor -> open *)
  8557. IF (toBase=fromBase) THEN i := 0;
  8558. ELSIF toBase = NIL THEN i := 1;
  8559. ELSIF toBase.SameType(fromBase) THEN i := 0
  8560. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8561. toBase := ArrayBase(toBase,Infinity);
  8562. IF (fromBase=toBase) THEN i := 0
  8563. ELSIF (toBase = NIL) THEN i:= 1
  8564. ELSIF (fromBase = NIL) THEN i := Infinity;
  8565. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8566. IF i < Infinity THEN i := i * 5 END;
  8567. END;
  8568. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8569. fromBase := ArrayBase(fromBase,Infinity);
  8570. IF (fromBase=toBase) THEN i := 0
  8571. ELSIF (toBase = NIL) THEN i := 1
  8572. ELSIF (fromBase = NIL) THEN i := Infinity;
  8573. ELSIF toBase.SameType(fromBase) THEN i := 0
  8574. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8575. IF i < Infinity THEN i := i * 5 END;
  8576. END;
  8577. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8578. IF i < Infinity THEN i := i * 5 END;
  8579. END;
  8580. IF i # Infinity THEN INC(i,2) END;
  8581. ELSIF (from.form = SyntaxTree.Static) THEN
  8582. (* static -> open *)
  8583. IF (toBase=fromBase) THEN i := 0;
  8584. ELSIF toBase = NIL THEN i := 1
  8585. ELSIF fromBase = NIL THEN i := Infinity
  8586. ELSIF toBase.SameType(fromBase) THEN i := 0
  8587. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8588. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8589. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8590. IF i < Infinity THEN i := i * 5 END;
  8591. END;
  8592. IF i # Infinity THEN INC(i,1) END;
  8593. ELSE HALT(100); (* unknown case *)
  8594. END;
  8595. RETURN i;
  8596. END MathArrayTypeDistance;
  8597. (** compute and return the distance of two array types
  8598. - return the distance of the base types
  8599. **)
  8600. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8601. VAR i: LONGINT;
  8602. BEGIN
  8603. i := Infinity;
  8604. IF from = to THEN
  8605. i := 0
  8606. ELSE
  8607. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8608. (*
  8609. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8610. i := TypeDistance(from.base, to.base);
  8611. IF i >= 0 THEN INC(i) END
  8612. ELSIF (from.mode = open) & (to.mode = open) THEN
  8613. i := TypeDistance(from.base, to.base);
  8614. *)
  8615. END;
  8616. RETURN i
  8617. END ArrayTypeDistance;
  8618. (** compute the signature distance of a procedure and an actual parameter list
  8619. - if any of the parameters are not compatible, the result is infinite
  8620. - add up and return the distance over all parameters
  8621. **)
  8622. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8623. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8624. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8625. BEGIN
  8626. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8627. result := Infinity
  8628. ELSE
  8629. formalParameter := procedureType.firstParameter;
  8630. i := 0;
  8631. result := 0;
  8632. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8633. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8634. actualParameter := actualParameters.GetExpression(i);
  8635. ASSERT(formalParameter.type # NIL);
  8636. IF (actualParameter.type = NIL) THEN distance := Infinity
  8637. ELSE
  8638. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8639. END;
  8640. IF distance = Infinity THEN
  8641. result := Infinity;
  8642. ELSE
  8643. to := formalParameter.type.resolved;
  8644. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8645. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8646. (* already handled varpar *)
  8647. (*
  8648. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8649. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8650. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8651. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8652. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8653. END;
  8654. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8655. result := Infinity
  8656. END;
  8657. *)
  8658. INC(result, distance);
  8659. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8660. INC(result, distance);
  8661. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8662. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8663. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8664. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8665. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8666. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8667. END;
  8668. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8669. result := Infinity
  8670. END;
  8671. ELSE
  8672. result := Infinity
  8673. END;
  8674. ELSE
  8675. INC(result,distance);
  8676. END;
  8677. END;
  8678. (*
  8679. Printout.Info("actual=", actualParameter);
  8680. Printout.Info("formal=", formalParameter);
  8681. TRACE(result);
  8682. *)
  8683. formalParameter := formalParameter.nextParameter; INC(i);
  8684. END;
  8685. END;
  8686. ASSERT(result >= 0);
  8687. RETURN result
  8688. END Distance;
  8689. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8690. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8691. BEGIN
  8692. IF right.numberParameters # (procedureType.numberParameters) THEN
  8693. result := Infinity
  8694. ELSE
  8695. formalParameter := procedureType.firstParameter;
  8696. rightParameter := right.firstParameter;
  8697. i := 0;
  8698. result := 0;
  8699. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8700. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8701. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8702. IF distance = Infinity THEN
  8703. result := Infinity;
  8704. ELSE
  8705. INC(result,distance);
  8706. END;
  8707. formalParameter := formalParameter.nextParameter;
  8708. rightParameter := rightParameter.nextParameter;
  8709. END;
  8710. END;
  8711. ASSERT(result >= 0);
  8712. RETURN result
  8713. END ProcedureTypeDistance;
  8714. (** compute and return the distance between two types, used for computation of signature distance
  8715. from -> to
  8716. **)
  8717. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8718. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8719. BEGIN
  8720. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8721. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8722. END;
  8723. i := Infinity;
  8724. IF from = to THEN
  8725. i := 0
  8726. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8727. ELSIF to.SameType(from) THEN
  8728. i := 0;
  8729. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8730. i := Infinity;
  8731. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8732. i := 10;
  8733. ELSIF (from IS SyntaxTree.StringType) THEN
  8734. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8735. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8736. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8737. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8738. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8739. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8740. i := 1
  8741. ELSIF (from IS SyntaxTree.NilType) THEN
  8742. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8743. (*
  8744. ELSIF (from = NoType) THEN
  8745. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8746. *)
  8747. ELSIF (from IS SyntaxTree.BasicType) THEN
  8748. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8749. IF varpar & (i # 0) THEN i := Infinity END;
  8750. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8751. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8752. ELSIF (from IS SyntaxTree.RecordType) THEN
  8753. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8754. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8755. IF to IS SyntaxTree.MathArrayType THEN
  8756. (*
  8757. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8758. i := Infinity;
  8759. ELSE
  8760. *)
  8761. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8762. (*
  8763. END;
  8764. *)
  8765. END
  8766. ELSIF (from IS SyntaxTree.PointerType) THEN
  8767. ptr := from(SyntaxTree.PointerType);
  8768. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8769. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8770. (* ELSE i := TypeDistance(ptr.base, to); *)
  8771. END
  8772. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8773. IF (to IS SyntaxTree.ProcedureType) THEN
  8774. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8775. END;
  8776. ELSIF (from IS SyntaxTree.PortType) THEN
  8777. IF (to IS SyntaxTree.PortType) THEN
  8778. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8779. i := 0;
  8780. END;
  8781. END;
  8782. (*no procedure test, procedure must be the same*)
  8783. END;
  8784. RETURN i
  8785. END TypeDistance;
  8786. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8787. BEGIN
  8788. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8789. END IsIntegerType;
  8790. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8791. BEGIN
  8792. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth)
  8793. OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)
  8794. OR IsPointerType(type)
  8795. )
  8796. END IsAddressType;
  8797. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8798. BEGIN
  8799. RETURN (type # NIL) & ((type.resolved IS SyntaxTree.IntegerType) & (type.resolved(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type.resolved IS SyntaxTree.SizeType))
  8800. END IsSizeType;
  8801. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8802. BEGIN
  8803. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8804. END IsSignedIntegerType;
  8805. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8806. BEGIN
  8807. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8808. END IsUnsignedIntegerType;
  8809. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8810. VAR result: BOOLEAN;
  8811. BEGIN
  8812. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8813. value := x.resolved(SyntaxTree.IntegerValue).value;
  8814. result := TRUE
  8815. ELSE
  8816. result := FALSE
  8817. END;
  8818. RETURN result
  8819. END IsIntegerValue;
  8820. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: Basic.Integer): BOOLEAN;
  8821. VAR result: BOOLEAN;
  8822. BEGIN
  8823. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8824. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8825. result := TRUE
  8826. ELSE
  8827. result := FALSE
  8828. END;
  8829. RETURN result
  8830. END IsEnumerationValue;
  8831. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8832. VAR result: BOOLEAN;
  8833. BEGIN
  8834. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8835. value := x.resolved(SyntaxTree.RealValue).value;
  8836. result := TRUE
  8837. ELSE
  8838. result := FALSE
  8839. END;
  8840. RETURN result
  8841. END IsRealValue;
  8842. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8843. VAR result: BOOLEAN;
  8844. BEGIN
  8845. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8846. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8847. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8848. result := TRUE
  8849. ELSE
  8850. result := FALSE
  8851. END;
  8852. RETURN result
  8853. END IsComplexValue;
  8854. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8855. VAR result: BOOLEAN;
  8856. BEGIN
  8857. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8858. value := x.resolved(SyntaxTree.CharacterValue).value;
  8859. result := TRUE
  8860. ELSE
  8861. result := FALSE
  8862. END;
  8863. RETURN result
  8864. END IsCharacterValue;
  8865. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8866. VAR result: BOOLEAN;
  8867. BEGIN
  8868. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8869. value := x.resolved(SyntaxTree.BooleanValue).value;
  8870. result := TRUE
  8871. ELSE
  8872. result := FALSE
  8873. END;
  8874. RETURN result
  8875. END IsBooleanValue;
  8876. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: Basic.Set): BOOLEAN;
  8877. VAR result: BOOLEAN;
  8878. BEGIN
  8879. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8880. value := x.resolved(SyntaxTree.SetValue).value;
  8881. result := TRUE
  8882. ELSE
  8883. result := FALSE
  8884. END;
  8885. RETURN result
  8886. END IsSetValue;
  8887. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8888. VAR result: BOOLEAN;
  8889. BEGIN
  8890. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8891. value := x.resolved(SyntaxTree.StringValue).value;
  8892. result := TRUE
  8893. ELSE
  8894. result := FALSE
  8895. END;
  8896. RETURN result
  8897. END IsStringValue;
  8898. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8899. BEGIN
  8900. x := x.resolved;
  8901. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8902. END Indexable;
  8903. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8904. BEGIN
  8905. RETURN t1.SameType(t2.resolved);
  8906. END SameType;
  8907. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8908. BEGIN
  8909. IF t IS SyntaxTree.MathArrayType THEN
  8910. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8911. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8912. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8913. DEC(max);
  8914. END;
  8915. ELSIF t IS SyntaxTree.ArrayType THEN
  8916. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8917. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8918. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8919. END;
  8920. END;
  8921. RETURN t;
  8922. END ArrayBase;
  8923. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8924. BEGIN
  8925. type := type.resolved;
  8926. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8927. base := type(SyntaxTree.ArrayType).arrayBase;
  8928. RETURN TRUE;
  8929. END;
  8930. RETURN FALSE;
  8931. END IsOpenArray;
  8932. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8933. BEGIN
  8934. type := type.resolved;
  8935. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8936. base := type(SyntaxTree.ArrayType).arrayBase;
  8937. dim := type(SyntaxTree.ArrayType).staticLength;
  8938. RETURN TRUE
  8939. ELSE
  8940. RETURN FALSE
  8941. END;
  8942. END IsStaticArray;
  8943. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8944. BEGIN
  8945. type := type.resolved;
  8946. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8947. base := type(SyntaxTree.ArrayType).arrayBase;
  8948. RETURN TRUE
  8949. ELSE
  8950. RETURN FALSE
  8951. END;
  8952. END IsDynamicArray;
  8953. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8954. VAR i: LONGINT;
  8955. BEGIN
  8956. i := 0;
  8957. t := t.resolved;
  8958. IF t IS SyntaxTree.MathArrayType THEN
  8959. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8960. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8961. END;
  8962. ELSIF t IS SyntaxTree.ArrayType THEN
  8963. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8964. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8965. END;
  8966. END;
  8967. RETURN i
  8968. END Dimension;
  8969. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8970. BEGIN
  8971. RETURN expression.assignable;
  8972. END IsVariable;
  8973. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8974. BEGIN
  8975. IF (symbol IS SyntaxTree.Parameter) THEN
  8976. WITH symbol: SyntaxTree.Parameter DO
  8977. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8978. END;
  8979. ELSE
  8980. RETURN FALSE
  8981. END;
  8982. END IsVariableParameter;
  8983. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8984. VAR result: BOOLEAN;
  8985. BEGIN
  8986. IF type = NIL THEN result := FALSE
  8987. ELSE
  8988. type := type.resolved;
  8989. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8990. END;
  8991. RETURN result
  8992. END IsPointerType;
  8993. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8994. VAR result: BOOLEAN;
  8995. BEGIN
  8996. IF type = NIL THEN result := FALSE
  8997. ELSE
  8998. type := type.resolved;
  8999. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe OR
  9000. (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9001. & type(SyntaxTree.MathArrayType).isUnsafe;
  9002. END;
  9003. RETURN result
  9004. END IsUnsafePointer;
  9005. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  9006. BEGIN
  9007. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  9008. END IsDisposable;
  9009. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  9010. VAR result: BOOLEAN;
  9011. BEGIN
  9012. IF type = NIL THEN result := FALSE
  9013. ELSE
  9014. type := type.resolved;
  9015. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  9016. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9017. result := result OR (type IS SyntaxTree.ObjectType);
  9018. END;
  9019. RETURN result
  9020. END IsPointerToRecord;
  9021. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  9022. VAR result: BOOLEAN;
  9023. BEGIN
  9024. IF type = NIL THEN result := FALSE
  9025. ELSE
  9026. type := type.resolved;
  9027. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  9028. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  9029. ;
  9030. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  9031. result := result OR (type IS SyntaxTree.ObjectType);
  9032. END;
  9033. RETURN result
  9034. END IsPointerToObject;
  9035. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  9036. BEGIN
  9037. IF type # NIL THEN
  9038. RETURN type.resolved.hasPointers
  9039. ELSE
  9040. RETURN FALSE
  9041. END;
  9042. END ContainsPointer;
  9043. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  9044. BEGIN
  9045. IF type = NIL THEN RETURN FALSE END;
  9046. type := type.resolved;
  9047. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  9048. END IsStringType;
  9049. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  9050. BEGIN
  9051. IF type = NIL THEN RETURN FALSE END;
  9052. type := type.resolved;
  9053. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  9054. END IsCharacterType;
  9055. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  9056. BEGIN
  9057. IF type = NIL THEN RETURN FALSE END;
  9058. type := type.resolved;
  9059. RETURN (type IS SyntaxTree.EnumerationType)
  9060. END IsEnumerationType;
  9061. (** cf. section "Type extension (base type)" in the language report **)
  9062. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9063. VAR result: BOOLEAN;
  9064. BEGIN
  9065. ASSERT(base # NIL); ASSERT(extension # NIL);
  9066. base := base.resolved; extension := extension.resolved;
  9067. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  9068. result := TRUE;
  9069. ELSE
  9070. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  9071. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  9072. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9073. END;
  9074. WHILE (extension # NIL) & (extension # base) DO
  9075. IF extension IS SyntaxTree.RecordType THEN
  9076. extension := extension(SyntaxTree.RecordType).baseType;
  9077. IF (extension # NIL) THEN extension := extension.resolved END;
  9078. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9079. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9080. END;
  9081. ELSE extension := NIL;
  9082. END;
  9083. END;
  9084. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9085. END;
  9086. RETURN result
  9087. END IsTypeExtension;
  9088. (** check if base is the base enumeration type of extension **)
  9089. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9090. BEGIN
  9091. base := base.resolved; extension := extension.resolved;
  9092. WHILE (extension # NIL) & (extension # base) DO
  9093. IF extension IS SyntaxTree.EnumerationType THEN
  9094. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9095. IF extension # NIL THEN extension := extension.resolved END;
  9096. ELSE
  9097. extension := NIL
  9098. END;
  9099. END;
  9100. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9101. END IsEnumerationExtension;
  9102. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9103. BEGIN
  9104. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9105. RETURN TRUE
  9106. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9107. RETURN TRUE
  9108. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9109. RETURN TRUE
  9110. ELSE
  9111. RETURN FALSE
  9112. END
  9113. END IsCallable;
  9114. (** compute and return the distance of two record types
  9115. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9116. **)
  9117. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9118. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9119. BEGIN
  9120. i := 0;
  9121. WHILE (from # NIL) & (from # to) DO
  9122. baseType := from.baseType;
  9123. IF (baseType # NIL) THEN
  9124. baseType := baseType.resolved;
  9125. IF baseType IS SyntaxTree.PointerType THEN
  9126. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9127. END;
  9128. IF baseType IS SyntaxTree.RecordType THEN
  9129. from := baseType(SyntaxTree.RecordType);
  9130. ELSE
  9131. from := NIL;
  9132. END;
  9133. ELSE
  9134. from := NIL
  9135. END;
  9136. INC(i)
  9137. END;
  9138. IF from = NIL THEN i := Infinity END;
  9139. RETURN i
  9140. END RecordTypeDistance;
  9141. (** compute and return the distance of two pointer types **)
  9142. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9143. BEGIN
  9144. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9145. RETURN Infinity;
  9146. ELSE
  9147. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9148. END;
  9149. END PointerTypeDistance;
  9150. (** check if expression contains a symbol designator pointing to a type declaration.
  9151. - if so then enter type declaration into typeDeclaration and return true else return false
  9152. **)
  9153. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9154. VAR result: BOOLEAN;
  9155. BEGIN
  9156. result := FALSE;
  9157. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9158. result := TRUE;
  9159. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9160. END;
  9161. RETURN result
  9162. END IsTypeDesignator;
  9163. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9164. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9165. VAR result: BOOLEAN;
  9166. BEGIN
  9167. type := type.resolved;
  9168. IF type IS SyntaxTree.PointerType THEN
  9169. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9170. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9171. result := TRUE
  9172. ELSE
  9173. result := type IS SyntaxTree.RecordType
  9174. END;
  9175. RETURN result
  9176. END IsExtensibleType;
  9177. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9178. BEGIN
  9179. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9180. (d IS SyntaxTree.SymbolDesignator) &
  9181. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9182. OR
  9183. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9184. END IsUnextensibleRecord;
  9185. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9186. BEGIN
  9187. IF IsUnextensibleRecord(d) THEN
  9188. RETURN FALSE
  9189. ELSE RETURN IsExtensibleType(d.type.resolved)
  9190. END;
  9191. END IsExtensibleDesignator;
  9192. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9193. BEGIN
  9194. type := type.resolved;
  9195. IF (type IS SyntaxTree.PointerType) THEN
  9196. RETURN TRUE
  9197. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9198. RETURN TRUE
  9199. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9200. RETURN TRUE
  9201. ELSIF (type IS SyntaxTree.BasicType) THEN
  9202. RETURN TRUE
  9203. END;
  9204. RETURN FALSE
  9205. END IsBasicType;
  9206. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9207. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9208. BEGIN
  9209. baseType := record.baseType;
  9210. IF (baseType # NIL) THEN
  9211. baseType := baseType.resolved;
  9212. IF (baseType IS SyntaxTree.PointerType) THEN
  9213. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9214. END;
  9215. END;
  9216. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9217. recordType := baseType(SyntaxTree.RecordType);
  9218. ELSE
  9219. recordType := NIL;
  9220. END;
  9221. RETURN recordType
  9222. END RecordBase;
  9223. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9224. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9225. BEGIN
  9226. baseRecord := RecordBase(scope.ownerRecord);
  9227. IF baseRecord = NIL THEN RETURN NIL END;
  9228. scope := baseRecord.recordScope;
  9229. procedureType := procedure.type.resolved;
  9230. IF procedure IS SyntaxTree.Operator THEN
  9231. operator := scope.firstOperator;
  9232. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9233. (*
  9234. Printout.Info("not same ",procedureType);
  9235. Printout.Info("with ",operator.type);
  9236. *)
  9237. operator := operator.nextOperator;
  9238. END;
  9239. super := operator;
  9240. ELSE
  9241. super := scope.firstProcedure;
  9242. WHILE (super # NIL) & (super.name # procedure.name) DO
  9243. super := super.nextProcedure;
  9244. END;
  9245. END;
  9246. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9247. RETURN super
  9248. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9249. RETURN super
  9250. ELSE
  9251. RETURN FindSuperProcedure(scope,procedure);
  9252. END;
  9253. END FindSuperProcedure;
  9254. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9255. VAR procedure: SyntaxTree.Procedure;
  9256. BEGIN
  9257. procedure := record.recordScope.constructor;
  9258. IF procedure = NIL THEN
  9259. record := RecordBase(record);
  9260. IF record # NIL THEN
  9261. procedure := GetConstructor(record)
  9262. END;
  9263. END;
  9264. RETURN procedure;
  9265. END GetConstructor;
  9266. (* enter a case into a list of cases in a sorted way and check for collision *)
  9267. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: Basic.Integer): BOOLEAN;
  9268. VAR prev,this,new: SyntaxTree.CaseConstant;
  9269. BEGIN
  9270. this := root;
  9271. prev := NIL;
  9272. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9273. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9274. RETURN FALSE
  9275. ELSE
  9276. IF (this # NIL) & (this.min = max+1) THEN
  9277. this.min := min
  9278. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9279. prev.max := min
  9280. ELSE
  9281. NEW(new); new.min := min; new.max := max;
  9282. new.next := this;
  9283. IF prev = NIL THEN
  9284. root := new;
  9285. ELSE
  9286. prev.next := new
  9287. END
  9288. END;
  9289. RETURN TRUE
  9290. END;
  9291. END EnterCase;
  9292. (** generate and return a new checker object, errors are entered into diagnostics **)
  9293. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9294. VAR checker: Checker;
  9295. BEGIN
  9296. NEW(checker, diagnostics,verboseErrorMessage,cooperative,system,symbolFileFormat,importCache,backend);
  9297. RETURN checker
  9298. END NewChecker;
  9299. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9300. VAR warnings: Warnings;
  9301. BEGIN
  9302. NEW(warnings, diagnostics); RETURN warnings;
  9303. END NewWarnings;
  9304. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9305. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9306. END IsRangeType;
  9307. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9308. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9309. END IsMathArrayType;
  9310. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9311. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9312. END IsArrayType;
  9313. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9314. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9315. END IsComplexType;
  9316. (** if a type is an array-structured object type *)
  9317. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9318. VAR recordType: SyntaxTree.RecordType;
  9319. BEGIN
  9320. IF type = NIL THEN
  9321. RETURN FALSE
  9322. ELSE
  9323. type := type.resolved;
  9324. IF type IS SyntaxTree.PointerType THEN
  9325. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9326. IF type IS SyntaxTree.RecordType THEN
  9327. recordType := type(SyntaxTree.RecordType);
  9328. RETURN recordType.isObject & recordType.HasArrayStructure()
  9329. ELSE
  9330. RETURN FALSE
  9331. END
  9332. ELSE
  9333. RETURN FALSE
  9334. END
  9335. END
  9336. END IsArrayStructuredObjectType;
  9337. (** the math array structure of a type
  9338. - for math arrays: the array itself
  9339. - for pointers: the math array structure of the pointer base
  9340. - for array-structured object types: the underlying structure
  9341. - for non-math arrays and all other types: NIL
  9342. **)
  9343. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9344. VAR
  9345. result: SyntaxTree.MathArrayType;
  9346. BEGIN
  9347. IF type = NIL THEN
  9348. result := NIL
  9349. ELSE
  9350. type := type.resolved;
  9351. IF type IS SyntaxTree.PointerType THEN
  9352. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9353. END;
  9354. IF type IS SyntaxTree.MathArrayType THEN
  9355. result := type(SyntaxTree.MathArrayType)
  9356. ELSIF type IS SyntaxTree.RecordType THEN
  9357. result := type(SyntaxTree.RecordType).arrayStructure
  9358. ELSE
  9359. result := NIL
  9360. END
  9361. END;
  9362. RETURN result
  9363. END MathArrayStructureOfType;
  9364. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: Basic.Integer): BOOLEAN;
  9365. VAR
  9366. result: BOOLEAN;
  9367. rangeExpression: SyntaxTree.RangeExpression;
  9368. BEGIN
  9369. IF x IS SyntaxTree.RangeExpression THEN
  9370. rangeExpression := x(SyntaxTree.RangeExpression);
  9371. result := TRUE;
  9372. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9373. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9374. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9375. ELSE
  9376. result := FALSE
  9377. END;
  9378. RETURN result
  9379. END IsStaticRange;
  9380. (** whether a type is a math array of tensor form **)
  9381. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9382. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9383. END IsTensor;
  9384. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9385. BEGIN
  9386. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9387. length := type(SyntaxTree.MathArrayType).staticLength;
  9388. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9389. RETURN TRUE
  9390. ELSE
  9391. RETURN FALSE
  9392. END;
  9393. END IsStaticMathArray;
  9394. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9395. BEGIN
  9396. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9397. END SymbolHasAddress;
  9398. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9399. BEGIN
  9400. RETURN
  9401. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9402. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9403. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9404. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9405. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9406. ;
  9407. END HasAddress;
  9408. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9409. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9410. BEGIN
  9411. IF (e IS SyntaxTree.Designator) THEN
  9412. d := e(SyntaxTree.Designator);
  9413. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9414. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9415. e := d.left;
  9416. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9417. END;
  9418. IF d # NIL THEN
  9419. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9420. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9421. END;
  9422. END;
  9423. RETURN FALSE;
  9424. END IsLocalVariable;
  9425. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9426. BEGIN
  9427. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9428. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9429. ELSE
  9430. RETURN TRUE
  9431. END;
  9432. END IsStaticProcedure;
  9433. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9434. CONST OptimizeMethodTable = FALSE;
  9435. BEGIN
  9436. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9437. END InMethodTable;
  9438. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9439. BEGIN
  9440. IF type = NIL THEN RETURN FALSE
  9441. ELSE
  9442. type := type.resolved;
  9443. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9444. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9445. END
  9446. END ReturnedAsParameter;
  9447. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9448. BEGIN
  9449. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9450. END StructuredReturnType;
  9451. END FoxSemanticChecker.
  9452. System.FreeDownTo FoxSemanticChecker ~