FoxSemanticChecker.Mod 402 KB

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