FoxSemanticChecker.Mod 402 KB

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