FoxSemanticChecker.Mod 401 KB

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