pdecsub.pas 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing of the procedures/functions
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pdecsub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { scanner }
  24. tokens,
  25. { symtable }
  26. symconst,symtype,symdef,symsym;
  27. type
  28. tpdflag=(
  29. pd_body, { directive needs a body }
  30. pd_implemen, { directive can be used in implementation section }
  31. pd_interface, { directive can be used in interface section }
  32. pd_object, { directive can be used with object declaration }
  33. pd_record, { directive can be used with record declaration }
  34. pd_procvar, { directive can be used with procvar declaration }
  35. pd_notobject, { directive can not be used with object declaration }
  36. pd_notrecord, { directive can not be used with record declaration }
  37. pd_notobjintf, { directive can not be used with interface declaration }
  38. pd_notprocvar, { directive can not be used with procvar declaration }
  39. pd_dispinterface,{ directive can be used with dispinterface methods }
  40. pd_cppobject, { directive can be used with cppclass }
  41. pd_objcclass, { directive can be used with objcclass }
  42. pd_objcprot, { directive can be used with objcprotocol }
  43. pd_nothelper, { directive can not be used with record/class helper declaration }
  44. pd_javaclass, { directive can be used with Java class }
  45. pd_intfjava { directive can be used with Java interface }
  46. );
  47. tpdflags=set of tpdflag;
  48. // flags of handle_calling_convention routine
  49. thccflag=(
  50. hcc_check, // perform checks and outup errors if found
  51. hcc_insert_hidden_paras // insert hidden parameters
  52. );
  53. thccflags=set of thccflag;
  54. const
  55. hcc_all=[hcc_check,hcc_insert_hidden_paras];
  56. function check_proc_directive(isprocvar:boolean):boolean;
  57. function proc_add_definition(var currpd:tprocdef):boolean;
  58. function proc_get_importname(pd:tprocdef):string;
  59. procedure proc_set_mangledname(pd:tprocdef);
  60. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  61. procedure parse_parameter_dec(pd:tabstractprocdef);
  62. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  63. procedure parse_var_proc_directives(sym:tsym);
  64. procedure parse_object_proc_directives(pd:tabstractprocdef);
  65. procedure parse_record_proc_directives(pd:tabstractprocdef);
  66. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  67. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  68. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean);
  69. { parse a record method declaration (not a (class) constructor/destructor) }
  70. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  71. procedure insert_record_hidden_paras(astruct: trecorddef);
  72. { helper functions - they insert nested objects hierarchy to the symtablestack
  73. with object hierarchy
  74. }
  75. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  76. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  77. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  78. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  79. implementation
  80. uses
  81. SysUtils,
  82. { common }
  83. cutils,
  84. { global }
  85. globtype,globals,verbose,constexp,
  86. systems,
  87. cpuinfo,
  88. { assembler }
  89. aasmbase,
  90. { symtable }
  91. symbase,symcpu,symtable,defutil,defcmp,
  92. { parameter handling }
  93. paramgr,cpupara,
  94. { pass 1 }
  95. fmodule,node,htypechk,ncon,ppu,nld,
  96. objcutil,
  97. { parser }
  98. scanner,
  99. syscinfo,
  100. pbase,pexpr,ptype,pdecl,pparautl,pgenutil
  101. {$ifdef jvm}
  102. ,pjvm
  103. {$endif}
  104. ;
  105. const
  106. { Please leave this here, this module should NOT use
  107. these variables.
  108. Declaring it as string here results in an error when compiling (PFV) }
  109. current_procinfo = 'error';
  110. function push_child_hierarchy(obj:tabstractrecorddef):integer;
  111. var
  112. _class,hp : tobjectdef;
  113. begin
  114. if obj.typ=recorddef then
  115. begin
  116. symtablestack.push(obj.symtable);
  117. result:=1;
  118. exit;
  119. end;
  120. result:=0;
  121. { insert class hierarchy in the reverse order }
  122. hp:=nil;
  123. repeat
  124. _class:=tobjectdef(obj);
  125. while _class.childof<>hp do
  126. _class:=_class.childof;
  127. hp:=_class;
  128. symtablestack.push(_class.symtable);
  129. inc(result);
  130. until hp=obj;
  131. end;
  132. function push_nested_hierarchy(obj:tabstractrecorddef):integer;
  133. begin
  134. result:=0;
  135. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  136. inc(result,push_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  137. inc(result,push_child_hierarchy(obj));
  138. end;
  139. function pop_child_hierarchy(obj:tabstractrecorddef):integer;
  140. var
  141. _class : tobjectdef;
  142. begin
  143. if obj.typ=recorddef then
  144. begin
  145. symtablestack.pop(obj.symtable);
  146. result:=1;
  147. exit;
  148. end;
  149. result:=0;
  150. _class:=tobjectdef(obj);
  151. while assigned(_class) do
  152. begin
  153. symtablestack.pop(_class.symtable);
  154. _class:=_class.childof;
  155. inc(result);
  156. end;
  157. end;
  158. function pop_nested_hierarchy(obj:tabstractrecorddef):integer;
  159. begin
  160. result:=pop_child_hierarchy(obj);
  161. if obj.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  162. inc(result,pop_nested_hierarchy(tabstractrecorddef(obj.owner.defowner)));
  163. end;
  164. procedure check_msg_para(p:TObject;arg:pointer);
  165. begin
  166. if (tsym(p).typ<>paravarsym) then
  167. exit;
  168. with tparavarsym(p) do
  169. begin
  170. { Count parameters }
  171. if (paranr>=10) then
  172. inc(plongint(arg)^);
  173. { First parameter must be var }
  174. if (paranr=10) and
  175. (varspez<>vs_var) then
  176. MessagePos(fileinfo,parser_e_ill_msg_param);
  177. end;
  178. end;
  179. procedure set_addr_param_regable(p:TObject;arg:pointer);
  180. begin
  181. if (tsym(p).typ<>paravarsym) then
  182. exit;
  183. with tparavarsym(p) do
  184. begin
  185. if (not needs_finalization) and
  186. paramanager.push_addr_param(varspez,vardef,tprocdef(arg).proccalloption) then
  187. varregable:=vr_addr;
  188. end;
  189. end;
  190. procedure parse_parameter_dec(pd:tabstractprocdef);
  191. {
  192. handle_procvar needs the same changes
  193. }
  194. type
  195. tppv = (pv_none,pv_proc,pv_func);
  196. var
  197. sc : TFPObjectList;
  198. hdef : tdef;
  199. arrayelementdef : tdef;
  200. vs : tparavarsym;
  201. i : longint;
  202. srsym : tsym;
  203. pv : tprocvardef;
  204. varspez : Tvarspez;
  205. defaultvalue : tconstsym;
  206. defaultrequired : boolean;
  207. old_block_type : tblock_type;
  208. currparast : tparasymtable;
  209. parseprocvar : tppv;
  210. locationstr : string;
  211. paranr : integer;
  212. dummytype : ttypesym;
  213. explicit_paraloc,
  214. need_array,
  215. is_univ: boolean;
  216. stoptions : TSingleTypeOptions;
  217. procedure handle_default_para_value;
  218. var
  219. convpd : tprocdef;
  220. doconv : tconverttype;
  221. nodetype : tnodetype;
  222. bt : tblock_type;
  223. begin
  224. { only allowed for types that can be represented by a
  225. constant expression }
  226. if try_to_consume(_EQ) then
  227. begin
  228. if (hdef.typ in [recorddef,variantdef,filedef,formaldef]) or
  229. is_object(hdef) or
  230. ((hdef.typ=arraydef) and
  231. not is_dynamic_array(hdef)) then
  232. Message1(type_e_invalid_default_value,FullTypeName(hdef,nil));
  233. vs:=tparavarsym(sc[0]);
  234. if sc.count>1 then
  235. Message(parser_e_default_value_only_one_para);
  236. if not(vs.varspez in [vs_value,vs_const,vs_constref]) then
  237. Message(parser_e_default_value_val_const);
  238. bt:=block_type;
  239. block_type:=bt_const;
  240. { prefix 'def' to the parameter name }
  241. defaultvalue:=ReadConstant('$def'+vs.name,vs.fileinfo,nodetype);
  242. block_type:=bt;
  243. if assigned(defaultvalue) then
  244. begin
  245. include(defaultvalue.symoptions,sp_internal);
  246. pd.parast.insert(defaultvalue);
  247. { check whether the default value is of the correct
  248. type }
  249. if compare_defs_ext(defaultvalue.constdef,hdef,nodetype,doconv,convpd,[])<=te_convert_operator then
  250. MessagePos2(defaultvalue.fileinfo,type_e_incompatible_types,FullTypeName(defaultvalue.constdef,hdef),FullTypeName(hdef,defaultvalue.constdef));
  251. end;
  252. defaultrequired:=true;
  253. end
  254. else
  255. begin
  256. if defaultrequired then
  257. Message1(parser_e_default_value_expected_for_para,vs.name);
  258. end;
  259. end;
  260. begin
  261. old_block_type:=block_type;
  262. explicit_paraloc:=false;
  263. consume(_LKLAMMER);
  264. { Delphi/Kylix supports nonsense like }
  265. { procedure p(); }
  266. if try_to_consume(_RKLAMMER) and
  267. not(m_tp7 in current_settings.modeswitches) then
  268. exit;
  269. { parsing a proc or procvar ? }
  270. currparast:=tparasymtable(pd.parast);
  271. { reset }
  272. sc:=TFPObjectList.create(false);
  273. defaultrequired:=false;
  274. paranr:=0;
  275. block_type:=bt_var;
  276. is_univ:=false;
  277. repeat
  278. parseprocvar:=pv_none;
  279. if try_to_consume(_VAR) then
  280. varspez:=vs_var
  281. else
  282. if try_to_consume(_CONST) then
  283. varspez:=vs_const
  284. else
  285. if (m_out in current_settings.modeswitches) and
  286. try_to_consume(_OUT) then
  287. varspez:=vs_out
  288. else
  289. if try_to_consume(_CONSTREF) then
  290. varspez:=vs_constref
  291. else
  292. if (m_mac in current_settings.modeswitches) and
  293. try_to_consume(_POINTPOINTPOINT) then
  294. begin
  295. include(pd.procoptions,po_varargs);
  296. break;
  297. end
  298. else
  299. if (m_nested_procvars in current_settings.modeswitches) and
  300. try_to_consume(_PROCEDURE) then
  301. begin
  302. parseprocvar:=pv_proc;
  303. varspez:=vs_const;
  304. end
  305. else
  306. if (m_nested_procvars in current_settings.modeswitches) and
  307. try_to_consume(_FUNCTION) then
  308. begin
  309. parseprocvar:=pv_func;
  310. varspez:=vs_const;
  311. end
  312. else
  313. varspez:=vs_value;
  314. defaultvalue:=nil;
  315. hdef:=nil;
  316. { read identifiers and insert with error type }
  317. sc.clear;
  318. repeat
  319. inc(paranr);
  320. vs:=cparavarsym.create(orgpattern,paranr*10,varspez,generrordef,[]);
  321. currparast.insert(vs);
  322. if assigned(vs.owner) then
  323. sc.add(vs)
  324. else
  325. vs.free;
  326. consume(_ID);
  327. until not try_to_consume(_COMMA);
  328. locationstr:='';
  329. { macpas anonymous procvar }
  330. if parseprocvar<>pv_none then
  331. begin
  332. { inline procvar definitions are always nested procvars }
  333. pv:=cprocvardef.create(normal_function_level+1);
  334. if token=_LKLAMMER then
  335. parse_parameter_dec(pv);
  336. if parseprocvar=pv_func then
  337. begin
  338. block_type:=bt_var_type;
  339. consume(_COLON);
  340. single_type(pv.returndef,[]);
  341. block_type:=bt_var;
  342. end;
  343. hdef:=pv;
  344. { possible proc directives }
  345. if check_proc_directive(true) then
  346. begin
  347. dummytype:=ctypesym.create('unnamed',hdef,true);
  348. parse_var_proc_directives(tsym(dummytype));
  349. dummytype.typedef:=nil;
  350. hdef.typesym:=nil;
  351. dummytype.free;
  352. end;
  353. { Add implicit hidden parameters and function result }
  354. handle_calling_convention(pv);
  355. {$ifdef jvm}
  356. { anonymous -> no name }
  357. jvm_create_procvar_class('',pv);
  358. {$endif}
  359. end
  360. else
  361. { read type declaration, force reading for value paras }
  362. if (token=_COLON) or (varspez=vs_value) then
  363. begin
  364. consume(_COLON);
  365. { check for an open array }
  366. need_array:=false;
  367. { bitpacked open array are not yet supported }
  368. if (token=_PACKED) and
  369. not(cs_bitpacking in current_settings.localswitches) then
  370. begin
  371. consume(_PACKED);
  372. need_array:=true;
  373. end;
  374. if (token=_ARRAY) or
  375. need_array then
  376. begin
  377. consume(_ARRAY);
  378. consume(_OF);
  379. { define range and type of range }
  380. hdef:=carraydef.create(0,-1,sizesinttype);
  381. { array of const ? }
  382. if (token=_CONST) and (m_objpas in current_settings.modeswitches) then
  383. begin
  384. consume(_CONST);
  385. srsym:=search_system_type('TVARREC');
  386. tarraydef(hdef).elementdef:=ttypesym(srsym).typedef;
  387. include(tarraydef(hdef).arrayoptions,ado_IsArrayOfConst);
  388. end
  389. else
  390. begin
  391. { define field type }
  392. if m_delphi in current_settings.modeswitches then
  393. stoptions:=[stoAllowSpecialization]
  394. else
  395. stoptions:=[];
  396. single_type(arrayelementdef,stoptions);
  397. tarraydef(hdef).elementdef:=arrayelementdef;
  398. end;
  399. end
  400. else
  401. begin
  402. if (m_mac in current_settings.modeswitches) then
  403. is_univ:=try_to_consume(_UNIV);
  404. if try_to_consume(_TYPE) then
  405. hdef:=ctypedformaltype
  406. else
  407. begin
  408. block_type:=bt_var_type;
  409. single_type(hdef,[stoAllowSpecialization]);
  410. block_type:=bt_var;
  411. end;
  412. { open string ? }
  413. if is_shortstring(hdef) then
  414. begin
  415. case varspez of
  416. vs_var,vs_out:
  417. begin
  418. { not 100% Delphi-compatible: type xstr=string[255] cannot
  419. become an openstring there, while here it can }
  420. if (cs_openstring in current_settings.moduleswitches) and
  421. (tstringdef(hdef).len=255) then
  422. hdef:=openshortstringtype
  423. end;
  424. vs_value:
  425. begin
  426. { value "openstring" parameters don't make sense (the
  427. original string can never be modified, so there's no
  428. use in passing its original length), so change these
  429. into regular shortstring parameters (seems to be what
  430. Delphi also does) }
  431. if is_open_string(hdef) then
  432. hdef:=cshortstringtype;
  433. end;
  434. end;
  435. end;
  436. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  437. begin
  438. if (idtoken=_LOCATION) then
  439. begin
  440. consume(_LOCATION);
  441. locationstr:=cstringpattern;
  442. consume(_CSTRING);
  443. end
  444. else
  445. begin
  446. if explicit_paraloc then
  447. Message(parser_e_paraloc_all_paras);
  448. locationstr:='';
  449. end;
  450. end
  451. else
  452. locationstr:='';
  453. { default parameter }
  454. if (m_default_para in current_settings.modeswitches) then
  455. handle_default_para_value;
  456. end;
  457. end
  458. else
  459. hdef:=cformaltype;
  460. { File types are only allowed for var and out parameters }
  461. if (hdef.typ=filedef) and
  462. not(varspez in [vs_out,vs_var]) then
  463. CGMessage(cg_e_file_must_call_by_reference);
  464. { Dispinterfaces are restricted to using only automatable types }
  465. if (pd.typ=procdef) and is_dispinterface(tprocdef(pd).struct) and
  466. not is_automatable(hdef) then
  467. Message1(type_e_not_automatable,hdef.typename);
  468. { univ cannot be used with types whose size is not known at compile
  469. time }
  470. if is_univ and
  471. not is_valid_univ_para_type(hdef) then
  472. Message1(parser_e_invalid_univ_para,hdef.typename);
  473. for i:=0 to sc.count-1 do
  474. begin
  475. vs:=tparavarsym(sc[i]);
  476. vs.univpara:=is_univ;
  477. { update varsym }
  478. vs.vardef:=hdef;
  479. vs.defaultconstsym:=defaultvalue;
  480. if (target_info.system in [system_powerpc_morphos,system_m68k_amiga]) then
  481. begin
  482. if locationstr<>'' then
  483. begin
  484. if sc.count>1 then
  485. Message(parser_e_paraloc_only_one_para);
  486. if (paranr>1) and not(explicit_paraloc) then
  487. Message(parser_e_paraloc_all_paras);
  488. explicit_paraloc:=true;
  489. include(vs.varoptions,vo_has_explicit_paraloc);
  490. if not(paramanager.parseparaloc(vs,locationstr)) then
  491. message(parser_e_illegal_explicit_paraloc);
  492. end
  493. else
  494. if explicit_paraloc then
  495. Message(parser_e_paraloc_all_paras);
  496. end;
  497. end;
  498. until not try_to_consume(_SEMICOLON);
  499. if explicit_paraloc then
  500. begin
  501. pd.has_paraloc_info:=callerside;
  502. include(pd.procoptions,po_explicitparaloc);
  503. end;
  504. { remove parasymtable from stack }
  505. sc.free;
  506. { reset object options }
  507. block_type:=old_block_type;
  508. consume(_RKLAMMER);
  509. end;
  510. function parse_proc_head(astruct:tabstractrecorddef;potype:tproctypeoption;isgeneric:boolean;genericdef:tdef;generictypelist:tfphashobjectlist;out pd:tprocdef):boolean;
  511. var
  512. hs : string;
  513. orgsp,sp,orgspnongen,spnongen : TIDString;
  514. dummysym,srsym : tsym;
  515. checkstack : psymtablestackitem;
  516. oldfilepos,
  517. classstartfilepos,
  518. procstartfilepos : tfileposinfo;
  519. i,
  520. index : longint;
  521. hadspecialize,
  522. firstpart,
  523. freegenericparams,
  524. found,
  525. searchagain : boolean;
  526. st,
  527. genericst: TSymtable;
  528. aprocsym : tprocsym;
  529. popclass : integer;
  530. ImplIntf : TImplementedInterface;
  531. old_parse_generic : boolean;
  532. old_current_structdef: tabstractrecorddef;
  533. old_current_genericdef,
  534. old_current_specializedef: tstoreddef;
  535. lasttoken,lastidtoken: ttoken;
  536. genericparams : tfphashobjectlist;
  537. procedure parse_operator_name;
  538. begin
  539. if (lasttoken in [first_overloaded..last_overloaded]) then
  540. begin
  541. optoken:=token;
  542. end
  543. else
  544. begin
  545. case lasttoken of
  546. _CARET:
  547. Message1(parser_e_overload_operator_failed,'**');
  548. _ID:
  549. case lastidtoken of
  550. _ENUMERATOR:optoken:=_OP_ENUMERATOR;
  551. _EXPLICIT:optoken:=_OP_EXPLICIT;
  552. _INC:optoken:=_OP_INC;
  553. _DEC:optoken:=_OP_DEC;
  554. else
  555. if (m_delphi in current_settings.modeswitches) then
  556. case lastidtoken of
  557. _IMPLICIT:optoken:=_ASSIGNMENT;
  558. _NEGATIVE:optoken:=_MINUS;
  559. _POSITIVE:optoken:=_PLUS;
  560. _LOGICALNOT:optoken:=_OP_NOT;
  561. _IN:optoken:=_OP_IN;
  562. _EQUAL:optoken:=_EQ;
  563. _NOTEQUAL:optoken:=_NE;
  564. _GREATERTHAN:optoken:=_GT;
  565. _GREATERTHANOREQUAL:optoken:=_GTE;
  566. _LESSTHAN:optoken:=_LT;
  567. _LESSTHANOREQUAL:optoken:=_LTE;
  568. _ADD:optoken:=_PLUS;
  569. _SUBTRACT:optoken:=_MINUS;
  570. _MULTIPLY:optoken:=_STAR;
  571. _DIVIDE:optoken:=_SLASH;
  572. _INTDIVIDE:optoken:=_OP_DIV;
  573. _MODULUS:optoken:=_OP_MOD;
  574. _LEFTSHIFT:optoken:=_OP_SHL;
  575. _RIGHTSHIFT:optoken:=_OP_SHR;
  576. _LOGICALAND:optoken:=_OP_AND;
  577. _LOGICALOR:optoken:=_OP_OR;
  578. _LOGICALXOR:optoken:=_OP_XOR;
  579. _BITWISEAND:optoken:=_OP_AND;
  580. _BITWISEOR:optoken:=_OP_OR;
  581. _BITWISEXOR:optoken:=_OP_XOR;
  582. else
  583. Message1(parser_e_overload_operator_failed,'');
  584. end
  585. else
  586. Message1(parser_e_overload_operator_failed,'');
  587. end
  588. else
  589. Message1(parser_e_overload_operator_failed,'');
  590. end;
  591. end;
  592. sp:=overloaded_names[optoken];
  593. orgsp:=sp;
  594. spnongen:=sp;
  595. orgspnongen:=orgsp;
  596. end;
  597. procedure consume_proc_name;
  598. var
  599. s : string;
  600. i : longint;
  601. sym : ttypesym;
  602. begin
  603. lasttoken:=token;
  604. lastidtoken:=idtoken;
  605. if assigned(genericparams) and freegenericparams then
  606. for i:=0 to genericparams.count-1 do
  607. begin
  608. sym:=ttypesym(genericparams[i]);
  609. if tstoreddef(sym.typedef).is_registered then
  610. begin
  611. sym.typedef.free;
  612. sym.typedef:=nil;
  613. end;
  614. sym.free;
  615. end;
  616. genericparams.free;
  617. genericparams:=nil;
  618. hadspecialize:=false;
  619. if potype=potype_operator then
  620. optoken:=NOTOKEN;
  621. if (potype=potype_operator) and (token<>_ID) then
  622. begin
  623. parse_operator_name;
  624. consume(token);
  625. end
  626. else
  627. begin
  628. sp:=pattern;
  629. orgsp:=orgpattern;
  630. spnongen:=sp;
  631. orgspnongen:=orgsp;
  632. if firstpart and
  633. not (m_delphi in current_settings.modeswitches) and
  634. (idtoken=_SPECIALIZE) then
  635. hadspecialize:=true;
  636. consume(_ID);
  637. if (isgeneric or (m_delphi in current_settings.modeswitches)) and
  638. (token in [_LT,_LSHARPBRACKET]) then
  639. begin
  640. consume(token);
  641. if token in [_GT,_RSHARPBRACKET] then
  642. message(type_e_type_id_expected)
  643. else
  644. begin
  645. genericparams:=parse_generic_parameters(true);
  646. if not assigned(genericparams) then
  647. internalerror(2015061201);
  648. if genericparams.count=0 then
  649. internalerror(2015061202);
  650. s:='';
  651. str(genericparams.count,s);
  652. spnongen:=sp;
  653. orgspnongen:=orgsp;
  654. sp:=sp+'$'+s;
  655. orgsp:=orgsp+'$'+s;
  656. end;
  657. if not try_to_consume(_GT) then
  658. consume(_RSHARPBRACKET);
  659. end;
  660. end;
  661. firstpart:=false;
  662. end;
  663. function search_object_name(sp:TIDString;gen_error:boolean):tsym;
  664. var
  665. storepos:tfileposinfo;
  666. srsymtable:TSymtable;
  667. begin
  668. storepos:=current_tokenpos;
  669. current_tokenpos:=procstartfilepos;
  670. searchsym(sp,result,srsymtable);
  671. if not assigned(result) then
  672. begin
  673. if gen_error then
  674. identifier_not_found(orgsp);
  675. result:=generrorsym;
  676. end;
  677. current_tokenpos:=storepos;
  678. end;
  679. procedure consume_generic_interface;
  680. var
  681. genparalist : tfpobjectlist;
  682. prettyname,
  683. specializename : ansistring;
  684. genname,
  685. ugenname : tidstring;
  686. gencount : string;
  687. begin
  688. consume(_LSHARPBRACKET);
  689. genparalist:=tfpobjectlist.create(false);
  690. if not parse_generic_specialization_types(genparalist,nil,prettyname,specializename) then
  691. srsym:=generrorsym
  692. else
  693. begin
  694. str(genparalist.count,gencount);
  695. genname:=sp+'$'+gencount;
  696. { ToDo: handle nested interfaces }
  697. genname:=generate_generic_name(genname,specializename,'');
  698. ugenname:=upper(genname);
  699. srsym:=search_object_name(ugenname,false);
  700. if not assigned(srsym) then
  701. begin
  702. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  703. srsym:=nil;
  704. exit;
  705. end;
  706. end;
  707. genparalist.free;
  708. consume(_RSHARPBRACKET);
  709. end;
  710. function handle_generic_interface:boolean;
  711. var
  712. i : longint;
  713. sym : ttypesym;
  714. typesrsym : tsym;
  715. typesrsymtable : tsymtable;
  716. specializename,
  717. prettyname: ansistring;
  718. error : boolean;
  719. genname,
  720. ugenname : tidstring;
  721. module : tmodule;
  722. begin
  723. result:=false;
  724. if not assigned(genericparams) then
  725. exit;
  726. specializename:='$';
  727. prettyname:='';
  728. error:=false;
  729. for i:=0 to genericparams.count-1 do
  730. begin
  731. sym:=ttypesym(genericparams[i]);
  732. { ToDo: position }
  733. if not searchsym(upper(sym.RealName),typesrsym,typesrsymtable) then
  734. begin
  735. message1(sym_e_id_not_found,sym.name);
  736. error:=true;
  737. continue;
  738. end;
  739. if typesrsym.typ<>typesym then
  740. begin
  741. message(type_e_type_id_expected);
  742. error:=true;
  743. continue;
  744. end;
  745. module:=find_module_from_symtable(ttypesym(typesrsym).typedef.owner);
  746. if not assigned(module) then
  747. internalerror(2016112803);
  748. specializename:=specializename+'_$'+hexstr(module.moduleid,8)+'$$'+ttypesym(typesrsym).typedef.unique_id_str;
  749. if i>0 then
  750. prettyname:=prettyname+',';
  751. prettyname:=prettyname+ttypesym(typesrsym).prettyname;
  752. end;
  753. result:=true;
  754. if error then
  755. begin
  756. srsym:=generrorsym;
  757. exit;
  758. end;
  759. { ToDo: handle nested interfaces }
  760. genname:=generate_generic_name(sp,specializename,'');
  761. ugenname:=upper(genname);
  762. srsym:=search_object_name(ugenname,false);
  763. if not assigned(srsym) then
  764. begin
  765. Message1(type_e_generic_declaration_does_not_match,sp+'<'+prettyname+'>');
  766. srsym:=generrorsym;
  767. end;
  768. end;
  769. procedure specialize_generic_interface;
  770. var
  771. node : tnode;
  772. begin
  773. node:=factor(false,[ef_type_only,ef_had_specialize]);
  774. if node.nodetype=typen then
  775. begin
  776. sp:=ttypenode(node).typedef.typesym.name;
  777. end
  778. else
  779. sp:='';
  780. end;
  781. begin
  782. sp:='';
  783. orgsp:='';
  784. spnongen:='';
  785. orgspnongen:='';
  786. { Save the position where this procedure really starts }
  787. procstartfilepos:=current_tokenpos;
  788. old_parse_generic:=parse_generic;
  789. firstpart:=true;
  790. result:=false;
  791. pd:=nil;
  792. aprocsym:=nil;
  793. srsym:=nil;
  794. genericparams:=nil;
  795. freegenericparams:=true;
  796. hadspecialize:=false;
  797. if not assigned(genericdef) then
  798. begin
  799. consume_proc_name;
  800. { examine interface map: function/procedure iname.functionname=locfuncname }
  801. if assigned(astruct) and
  802. (astruct.typ=objectdef) and
  803. assigned(tobjectdef(astruct).ImplementedInterfaces) and
  804. (tobjectdef(astruct).ImplementedInterfaces.count>0) and
  805. (
  806. (token=_POINT) or
  807. (
  808. hadspecialize and
  809. (token=_ID)
  810. )
  811. ) then
  812. begin
  813. if hadspecialize and (token=_ID) then
  814. specialize_generic_interface;
  815. consume(_POINT);
  816. if hadspecialize or not handle_generic_interface then
  817. srsym:=search_object_name(sp,true);
  818. { qualifier is interface? }
  819. ImplIntf:=nil;
  820. if assigned(srsym) and
  821. (srsym.typ=typesym) and
  822. (ttypesym(srsym).typedef.typ=objectdef) then
  823. ImplIntf:=find_implemented_interface(tobjectdef(astruct),tobjectdef(ttypesym(srsym).typedef));
  824. if ImplIntf=nil then
  825. begin
  826. Message(parser_e_interface_id_expected);
  827. { error recovery }
  828. consume(_ID);
  829. if try_to_consume(_EQ) then
  830. consume(_ID);
  831. exit;
  832. end
  833. else
  834. { in case of a generic or specialized interface we need to use the
  835. name of the def instead of the symbol, so that always the correct
  836. name is used }
  837. if [df_generic,df_specialization]*ttypesym(srsym).typedef.defoptions<>[] then
  838. sp:=tobjectdef(ttypesym(srsym).typedef).objname^;
  839. { must be a directly implemented interface }
  840. if Assigned(ImplIntf.ImplementsGetter) then
  841. Message2(parser_e_implements_no_mapping,ImplIntf.IntfDef.typename,astruct.objrealname^);
  842. consume(_ID);
  843. { Create unique name <interface>.<method> }
  844. hs:=sp+'.'+pattern;
  845. consume(_EQ);
  846. if assigned(ImplIntf) and
  847. (token=_ID) then
  848. ImplIntf.AddMapping(hs,pattern);
  849. consume(_ID);
  850. result:=true;
  851. exit;
  852. end;
  853. if assigned(genericparams) and assigned(current_genericdef) then
  854. Message(parser_f_no_generic_inside_generic);
  855. { method ? }
  856. srsym:=nil;
  857. if not assigned(astruct) and
  858. (symtablestack.top.symtablelevel=main_program_level) and
  859. try_to_consume(_POINT) then
  860. begin
  861. repeat
  862. classstartfilepos:=procstartfilepos;
  863. searchagain:=false;
  864. { throw the error at the right location }
  865. oldfilepos:=current_filepos;
  866. current_filepos:=procstartfilepos;
  867. if not assigned(astruct) and not assigned(srsym) then
  868. srsym:=search_object_name(sp,true);
  869. current_filepos:=oldfilepos;
  870. { consume proc name }
  871. procstartfilepos:=current_tokenpos;
  872. consume_proc_name;
  873. { qualifier is class name ? }
  874. if (srsym.typ=typesym) and
  875. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  876. begin
  877. astruct:=tabstractrecorddef(ttypesym(srsym).typedef);
  878. if (token<>_POINT) then
  879. if (potype in [potype_class_constructor,potype_class_destructor]) then
  880. sp:=lower(sp)
  881. else
  882. if (potype=potype_operator) and (optoken=NOTOKEN) then
  883. parse_operator_name;
  884. srsym:=tsym(astruct.symtable.Find(sp));
  885. if assigned(srsym) then
  886. begin
  887. if srsym.typ=procsym then
  888. aprocsym:=tprocsym(srsym)
  889. else
  890. if (srsym.typ=typesym) and
  891. (ttypesym(srsym).typedef.typ in [objectdef,recorddef]) then
  892. begin
  893. searchagain:=true;
  894. consume(_POINT);
  895. end
  896. else
  897. begin
  898. { we use a different error message for tp7 so it looks more compatible }
  899. if (m_fpc in current_settings.modeswitches) then
  900. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  901. else
  902. Message(parser_e_methode_id_expected);
  903. { rename the name to an unique name to avoid an
  904. error when inserting the symbol in the symtable }
  905. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  906. end;
  907. end
  908. else
  909. begin
  910. MessagePos(procstartfilepos,parser_e_methode_id_expected);
  911. { recover by making it a normal procedure instead of method }
  912. astruct:=nil;
  913. end;
  914. end
  915. else
  916. MessagePos(classstartfilepos,parser_e_class_id_expected);
  917. until not searchagain;
  918. end
  919. else
  920. begin
  921. { check for constructor/destructor/class operators which are not allowed here }
  922. if (not parse_only) and
  923. ((potype in [potype_constructor,potype_destructor,
  924. potype_class_constructor,potype_class_destructor]) or
  925. ((potype=potype_operator) and (m_delphi in current_settings.modeswitches))) then
  926. Message(parser_e_only_methods_allowed);
  927. repeat
  928. { only 1 class constructor and destructor is allowed in the class and
  929. the check was already done with oo_has_class_constructor or
  930. oo_has_class_destructor -> skip searching
  931. (bug #28801) }
  932. if (potype in [potype_class_constructor,potype_class_destructor]) then
  933. break;
  934. searchagain:=false;
  935. current_tokenpos:=procstartfilepos;
  936. if (potype=potype_operator)and(optoken=NOTOKEN) then
  937. parse_operator_name;
  938. srsym:=tsym(symtablestack.top.Find(sp));
  939. { Also look in the globalsymtable if we didn't found
  940. the symbol in the localsymtable }
  941. if not assigned(srsym) and
  942. not(parse_only) and
  943. (symtablestack.top=current_module.localsymtable) and
  944. assigned(current_module.globalsymtable) then
  945. srsym:=tsym(current_module.globalsymtable.Find(sp));
  946. { Check if overloaded is a procsym }
  947. if assigned(srsym) then
  948. begin
  949. if srsym.typ=procsym then
  950. aprocsym:=tprocsym(srsym)
  951. else
  952. begin
  953. { when the other symbol is a unit symbol then hide the unit
  954. symbol, this is not supported in tp7 }
  955. if not(m_tp7 in current_settings.modeswitches) and
  956. (srsym.typ=unitsym) then
  957. begin
  958. HideSym(srsym);
  959. searchagain:=true;
  960. end
  961. else
  962. if (m_delphi in current_settings.modeswitches) and
  963. (srsym.typ=absolutevarsym) and
  964. ([vo_is_funcret,vo_is_result]*tabstractvarsym(srsym).varoptions=[vo_is_funcret]) then
  965. begin
  966. HideSym(srsym);
  967. searchagain:=true;
  968. end
  969. else
  970. begin
  971. { we use a different error message for tp7 so it looks more compatible }
  972. if (m_fpc in current_settings.modeswitches) then
  973. Message1(parser_e_overloaded_no_procedure,srsym.realname)
  974. else
  975. Message1(sym_e_duplicate_id,srsym.realname);
  976. { rename the name to an unique name to avoid an
  977. error when inserting the symbol in the symtable }
  978. orgsp:=orgsp+'$'+tostr(current_filepos.line);
  979. end;
  980. end;
  981. end;
  982. until not searchagain;
  983. end;
  984. { test again if assigned, it can be reset to recover }
  985. if not assigned(aprocsym) then
  986. begin
  987. { create a new procsym and set the real filepos }
  988. current_tokenpos:=procstartfilepos;
  989. { for operator we have only one procsym for each overloaded
  990. operation }
  991. if (potype=potype_operator) then
  992. begin
  993. aprocsym:=Tprocsym(symtablestack.top.Find(sp));
  994. if aprocsym=nil then
  995. aprocsym:=cprocsym.create('$'+sp);
  996. end
  997. else
  998. if (potype in [potype_class_constructor,potype_class_destructor]) then
  999. aprocsym:=cprocsym.create('$'+lower(sp))
  1000. else
  1001. aprocsym:=cprocsym.create(orgsp);
  1002. symtablestack.top.insert(aprocsym);
  1003. end;
  1004. end;
  1005. { to get the correct symtablelevel we must ignore ObjectSymtables }
  1006. st:=nil;
  1007. checkstack:=symtablestack.stack;
  1008. while assigned(checkstack) do
  1009. begin
  1010. st:=checkstack^.symtable;
  1011. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  1012. break;
  1013. checkstack:=checkstack^.next;
  1014. end;
  1015. pd:=cprocdef.create(st.symtablelevel+1,not assigned(genericdef));
  1016. pd.struct:=astruct;
  1017. pd.procsym:=aprocsym;
  1018. pd.proctypeoption:=potype;
  1019. if assigned(genericparams) then
  1020. begin
  1021. include(pd.defoptions,df_generic);
  1022. { push the parameter symtable so that constraint definitions are added
  1023. there and not in the owner symtable }
  1024. symtablestack.push(pd.parast);
  1025. { register the parameters }
  1026. for i:=0 to genericparams.count-1 do
  1027. begin
  1028. ttypesym(genericparams[i]).register_sym;
  1029. tstoreddef(ttypesym(genericparams[i]).typedef).register_def;
  1030. end;
  1031. insert_generic_parameter_types(pd,nil,genericparams);
  1032. symtablestack.pop(pd.parast);
  1033. freegenericparams:=false;
  1034. parse_generic:=true;
  1035. { also generate a dummy symbol if none exists already }
  1036. if assigned(astruct) then
  1037. dummysym:=tsym(astruct.symtable.find(spnongen))
  1038. else
  1039. begin
  1040. dummysym:=tsym(symtablestack.top.find(spnongen));
  1041. if not assigned(dummysym) and
  1042. (symtablestack.top=current_module.localsymtable) and
  1043. assigned(current_module.globalsymtable) then
  1044. dummysym:=tsym(current_module.globalsymtable.find(spnongen));
  1045. end;
  1046. if not assigned(dummysym) then
  1047. begin
  1048. dummysym:=ctypesym.create(orgspnongen,cundefineddef.create(true),true);
  1049. if assigned(astruct) then
  1050. astruct.symtable.insert(dummysym)
  1051. else
  1052. symtablestack.top.insert(dummysym);
  1053. end;
  1054. include(dummysym.symoptions,sp_generic_dummy);
  1055. { start token recorder for the declaration }
  1056. pd.init_genericdecl;
  1057. current_scanner.startrecordtokens(pd.genericdecltokenbuf);
  1058. end;
  1059. if assigned(genericdef) and not assigned(genericparams) then
  1060. insert_generic_parameter_types(pd,tstoreddef(genericdef),generictypelist);
  1061. { methods inherit df_generic or df_specialization from the objectdef }
  1062. if assigned(pd.struct) and
  1063. (pd.parast.symtablelevel=normal_function_level) then
  1064. begin
  1065. if (df_generic in pd.struct.defoptions) then
  1066. begin
  1067. include(pd.defoptions,df_generic);
  1068. parse_generic:=true;
  1069. end;
  1070. if (df_specialization in pd.struct.defoptions) then
  1071. begin
  1072. if assigned(current_specializedef) then
  1073. begin
  1074. include(pd.defoptions,df_specialization);
  1075. { Find corresponding genericdef, we need it later to
  1076. replay the tokens to generate the body }
  1077. if not assigned(pd.struct.genericdef) then
  1078. internalerror(200512113);
  1079. genericst:=pd.struct.genericdef.GetSymtable(gs_record);
  1080. if not assigned(genericst) then
  1081. internalerror(200512114);
  1082. { when searching for the correct procdef to use as genericdef we need to ignore
  1083. everything except procdefs so that we can find the correct indices }
  1084. index:=0;
  1085. found:=false;
  1086. for i:=0 to pd.owner.deflist.count-1 do
  1087. begin
  1088. if tdef(pd.owner.deflist[i]).typ<>procdef then
  1089. continue;
  1090. if pd.owner.deflist[i]=pd then
  1091. begin
  1092. found:=true;
  1093. break;
  1094. end;
  1095. inc(index);
  1096. end;
  1097. if not found then
  1098. internalerror(2014052301);
  1099. for i:=0 to genericst.deflist.count-1 do
  1100. begin
  1101. if tdef(genericst.deflist[i]).typ<>procdef then
  1102. continue;
  1103. if index=0 then
  1104. pd.genericdef:=tstoreddef(genericst.deflist[i]);
  1105. dec(index);
  1106. end;
  1107. if not assigned(pd.genericdef) or
  1108. (pd.genericdef.typ<>procdef) then
  1109. internalerror(200512115);
  1110. end
  1111. else
  1112. Message(parser_e_explicit_method_implementation_for_specializations_not_allowed);
  1113. end;
  1114. end;
  1115. { methods need to be exported }
  1116. if assigned(astruct) and
  1117. (
  1118. (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) or
  1119. (symtablestack.top.symtablelevel=main_program_level)
  1120. ) then
  1121. include(pd.procoptions,po_global);
  1122. { symbol options that need to be kept per procdef }
  1123. pd.fileinfo:=procstartfilepos;
  1124. pd.visibility:=symtablestack.top.currentvisibility;
  1125. if symtablestack.top.currentlyoptional then
  1126. include(pd.procoptions,po_optional);
  1127. { parse parameters }
  1128. if token=_LKLAMMER then
  1129. begin
  1130. old_current_structdef:=nil;
  1131. old_current_genericdef:=current_genericdef;
  1132. old_current_specializedef:=nil;
  1133. { Add ObjectSymtable to be able to find nested type definitions }
  1134. popclass:=0;
  1135. if assigned(pd.struct) and
  1136. (pd.parast.symtablelevel>=normal_function_level) and
  1137. not(symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1138. begin
  1139. popclass:=push_nested_hierarchy(pd.struct);
  1140. old_current_structdef:=current_structdef;
  1141. old_current_specializedef:=current_specializedef;
  1142. current_structdef:=pd.struct;
  1143. if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1144. current_genericdef:=current_structdef;
  1145. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1146. current_specializedef:=current_structdef;
  1147. end;
  1148. if pd.is_generic then
  1149. current_genericdef:=pd;
  1150. { Add parameter symtable }
  1151. if pd.parast.symtabletype<>staticsymtable then
  1152. symtablestack.push(pd.parast);
  1153. parse_parameter_dec(pd);
  1154. if pd.parast.symtabletype<>staticsymtable then
  1155. symtablestack.pop(pd.parast);
  1156. current_genericdef:=old_current_genericdef;
  1157. if popclass>0 then
  1158. begin
  1159. current_structdef:=old_current_structdef;
  1160. current_specializedef:=old_current_specializedef;
  1161. dec(popclass,pop_nested_hierarchy(pd.struct));
  1162. if popclass<>0 then
  1163. internalerror(201011260); // 11 nov 2010 index 0
  1164. end;
  1165. end;
  1166. parse_generic:=old_parse_generic;
  1167. result:=true;
  1168. end;
  1169. procedure parse_proc_dec_finish(pd:tprocdef;isclassmethod:boolean);
  1170. var
  1171. locationstr: string;
  1172. i: integer;
  1173. found: boolean;
  1174. procedure read_returndef(pd: tprocdef);
  1175. var
  1176. popclass: integer;
  1177. old_parse_generic: boolean;
  1178. old_current_structdef: tabstractrecorddef;
  1179. old_current_genericdef,
  1180. old_current_specializedef: tstoreddef;
  1181. begin
  1182. old_parse_generic:=parse_generic;
  1183. { Add ObjectSymtable to be able to find generic type definitions }
  1184. popclass:=0;
  1185. old_current_structdef:=nil;
  1186. old_current_genericdef:=current_genericdef;
  1187. old_current_specializedef:=current_specializedef;
  1188. current_genericdef:=nil;
  1189. current_specializedef:=nil;
  1190. if assigned(pd.struct) and
  1191. (pd.parast.symtablelevel>=normal_function_level) and
  1192. not (symtablestack.top.symtabletype in [ObjectSymtable,recordsymtable]) then
  1193. begin
  1194. popclass:=push_nested_hierarchy(pd.struct);
  1195. old_current_structdef:=current_structdef;
  1196. current_structdef:=pd.struct;
  1197. end;
  1198. if df_generic in pd.defoptions then
  1199. begin
  1200. if pd.is_generic then
  1201. current_genericdef:=pd
  1202. else if assigned(pd.struct) then
  1203. current_genericdef:=pd.struct
  1204. else
  1205. internalerror(2016090202);
  1206. end;
  1207. if df_specialization in pd.defoptions then
  1208. begin
  1209. if pd.is_specialization then
  1210. current_specializedef:=pd
  1211. else if assigned(pd.struct) then
  1212. current_specializedef:=pd.struct
  1213. else
  1214. internalerror(2016090203);
  1215. end;
  1216. parse_generic:=(df_generic in pd.defoptions);
  1217. if pd.is_generic or pd.is_specialization then
  1218. symtablestack.push(pd.parast);
  1219. single_type(pd.returndef,[stoAllowSpecialization]);
  1220. // Issue #24863, enabled only for the main progra commented out for now because it breaks building of RTL and needs extensive
  1221. // testing and/or RTL patching.
  1222. {
  1223. if ((pd.returndef=cvarianttype) or (pd.returndef=colevarianttype)) and
  1224. not(cs_compilesystem in current_settings.moduleswitches) then
  1225. current_module.flags:=current_module.flags or uf_uses_variants;
  1226. }
  1227. if is_dispinterface(pd.struct) and not is_automatable(pd.returndef) then
  1228. Message1(type_e_not_automatable,pd.returndef.typename);
  1229. if pd.is_generic or pd.is_specialization then
  1230. symtablestack.pop(pd.parast);
  1231. if popclass>0 then
  1232. begin
  1233. current_structdef:=old_current_structdef;
  1234. dec(popclass,pop_nested_hierarchy(pd.struct));
  1235. if popclass<>0 then
  1236. internalerror(201012020);
  1237. end;
  1238. current_genericdef:=old_current_genericdef;
  1239. current_specializedef:=old_current_specializedef;
  1240. parse_generic:=old_parse_generic;
  1241. end;
  1242. begin
  1243. locationstr:='';
  1244. case pd.proctypeoption of
  1245. potype_procedure:
  1246. begin
  1247. pd.returndef:=voidtype;
  1248. if isclassmethod then
  1249. include(pd.procoptions,po_classmethod);
  1250. end;
  1251. potype_function:
  1252. begin
  1253. if try_to_consume(_COLON) then
  1254. begin
  1255. read_returndef(pd);
  1256. if (target_info.system in [system_m68k_amiga]) then
  1257. begin
  1258. if (idtoken=_LOCATION) then
  1259. begin
  1260. if po_explicitparaloc in pd.procoptions then
  1261. begin
  1262. consume(_LOCATION);
  1263. locationstr:=cstringpattern;
  1264. consume(_CSTRING);
  1265. end
  1266. else
  1267. { I guess this needs a new message... (KB) }
  1268. Message(parser_e_paraloc_all_paras);
  1269. end
  1270. else
  1271. begin
  1272. if po_explicitparaloc in pd.procoptions then
  1273. { assign default locationstr, if none specified }
  1274. { and we've arguments with explicit paraloc }
  1275. locationstr:='D0';
  1276. end;
  1277. end;
  1278. end
  1279. else
  1280. begin
  1281. if (
  1282. parse_only and
  1283. not(is_interface(pd.struct))
  1284. ) or
  1285. (m_repeat_forward in current_settings.modeswitches) then
  1286. begin
  1287. consume(_COLON);
  1288. consume_all_until(_SEMICOLON);
  1289. end;
  1290. end;
  1291. if isclassmethod then
  1292. include(pd.procoptions,po_classmethod);
  1293. end;
  1294. potype_constructor,
  1295. potype_class_constructor:
  1296. begin
  1297. if not isclassmethod and
  1298. assigned(pd) and
  1299. assigned(pd.struct) then
  1300. begin
  1301. { Set return type, class constructors return the
  1302. created instance, object constructors return boolean }
  1303. if is_class(pd.struct) or
  1304. is_record(pd.struct) or
  1305. is_javaclass(pd.struct) then
  1306. pd.returndef:=pd.struct
  1307. else
  1308. if is_objectpascal_helper(pd.struct) then
  1309. pd.returndef:=tobjectdef(pd.struct).extendeddef
  1310. else
  1311. {$ifdef CPU64bitaddr}
  1312. pd.returndef:=bool64type;
  1313. {$else CPU64bitaddr}
  1314. pd.returndef:=bool32type;
  1315. {$endif CPU64bitaddr}
  1316. end
  1317. else
  1318. pd.returndef:=voidtype;
  1319. end;
  1320. potype_class_destructor,
  1321. potype_destructor:
  1322. begin
  1323. if assigned(pd) then
  1324. pd.returndef:=voidtype;
  1325. end;
  1326. potype_operator:
  1327. begin
  1328. { operators always need to be searched in all units (that
  1329. contain operators) }
  1330. include(pd.procoptions,po_overload);
  1331. pd.procsym.owner.includeoption(sto_has_operator);
  1332. if pd.parast.symtablelevel>normal_function_level then
  1333. Message(parser_e_no_local_operator);
  1334. if isclassmethod then
  1335. begin
  1336. include(pd.procoptions,po_classmethod);
  1337. { any class operator is also static }
  1338. include(pd.procoptions,po_staticmethod);
  1339. end;
  1340. if token<>_ID then
  1341. begin
  1342. if not(m_result in current_settings.modeswitches) then
  1343. consume(_ID);
  1344. end
  1345. else
  1346. begin
  1347. pd.resultname:=stringdup(orgpattern);
  1348. consume(_ID);
  1349. end;
  1350. if not try_to_consume(_COLON) then
  1351. begin
  1352. consume(_COLON);
  1353. pd.returndef:=generrordef;
  1354. consume_all_until(_SEMICOLON);
  1355. end
  1356. else
  1357. begin
  1358. read_returndef(pd);
  1359. { check that class operators have either return type of structure or }
  1360. { at least one argument of that type }
  1361. if (po_classmethod in pd.procoptions) and
  1362. (pd.returndef <> pd.struct) then
  1363. begin
  1364. found:=false;
  1365. for i := 0 to pd.parast.SymList.Count - 1 do
  1366. if tparavarsym(pd.parast.SymList[i]).vardef=pd.struct then
  1367. begin
  1368. found:=true;
  1369. break;
  1370. end;
  1371. if not found then
  1372. if assigned(pd.struct) then
  1373. Message1(parser_e_at_least_one_argument_must_be_of_type,pd.struct.RttiName)
  1374. else
  1375. MessagePos(pd.fileinfo,type_e_type_id_expected);
  1376. end;
  1377. if (optoken in [_ASSIGNMENT,_OP_EXPLICIT]) and
  1378. equal_defs(pd.returndef,tparavarsym(pd.parast.SymList[0]).vardef) and
  1379. (pd.returndef.typ<>undefineddef) and (tparavarsym(pd.parast.SymList[0]).vardef.typ<>undefineddef) then
  1380. message(parser_e_no_such_assignment)
  1381. else if not isoperatoracceptable(pd,optoken) then
  1382. Message(parser_e_overload_impossible);
  1383. end;
  1384. end;
  1385. else
  1386. internalerror(2015052202);
  1387. end;
  1388. { file types can't be function results }
  1389. if assigned(pd) and
  1390. (pd.returndef.typ=filedef) then
  1391. message(parser_e_illegal_function_result);
  1392. { support procedure proc stdcall export; }
  1393. if not(check_proc_directive(false)) then
  1394. begin
  1395. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1396. begin
  1397. message(parser_e_field_not_allowed_here);
  1398. consume_all_until(_SEMICOLON);
  1399. end;
  1400. consume(_SEMICOLON);
  1401. end;
  1402. if locationstr<>'' then
  1403. begin
  1404. if not(paramanager.parsefuncretloc(pd,upper(locationstr))) then
  1405. { I guess this needs a new message... (KB) }
  1406. message(parser_e_illegal_explicit_paraloc);
  1407. end;
  1408. end;
  1409. function parse_proc_dec(isclassmethod:boolean;astruct:tabstractrecorddef;isgeneric:boolean):tprocdef;
  1410. var
  1411. pd : tprocdef;
  1412. old_block_type : tblock_type;
  1413. recover : boolean;
  1414. procedure finish_intf_mapping;
  1415. begin
  1416. if token=_COLON then
  1417. begin
  1418. message(parser_e_field_not_allowed_here);
  1419. consume_all_until(_SEMICOLON);
  1420. end;
  1421. consume(_SEMICOLON);
  1422. end;
  1423. begin
  1424. pd:=nil;
  1425. recover:=false;
  1426. case token of
  1427. _FUNCTION :
  1428. begin
  1429. consume(_FUNCTION);
  1430. if parse_proc_head(astruct,potype_function,isgeneric,nil,nil,pd) then
  1431. begin
  1432. { pd=nil when it is a interface mapping }
  1433. if assigned(pd) then
  1434. parse_proc_dec_finish(pd,isclassmethod)
  1435. else
  1436. finish_intf_mapping;
  1437. end
  1438. else
  1439. begin
  1440. { recover }
  1441. consume(_COLON);
  1442. consume_all_until(_SEMICOLON);
  1443. recover:=true;
  1444. end;
  1445. end;
  1446. _PROCEDURE :
  1447. begin
  1448. consume(_PROCEDURE);
  1449. if parse_proc_head(astruct,potype_procedure,isgeneric,nil,nil,pd) then
  1450. begin
  1451. { pd=nil when it is an interface mapping }
  1452. if assigned(pd) then
  1453. parse_proc_dec_finish(pd,isclassmethod)
  1454. else
  1455. finish_intf_mapping;
  1456. end
  1457. else
  1458. recover:=true;
  1459. end;
  1460. _CONSTRUCTOR :
  1461. begin
  1462. consume(_CONSTRUCTOR);
  1463. if isclassmethod then
  1464. recover:=not parse_proc_head(astruct,potype_class_constructor,false,nil,nil,pd)
  1465. else
  1466. recover:=not parse_proc_head(astruct,potype_constructor,false,nil,nil,pd);
  1467. if not recover then
  1468. parse_proc_dec_finish(pd,isclassmethod);
  1469. end;
  1470. _DESTRUCTOR :
  1471. begin
  1472. consume(_DESTRUCTOR);
  1473. if isclassmethod then
  1474. recover:=not parse_proc_head(astruct,potype_class_destructor,false,nil,nil,pd)
  1475. else
  1476. recover:=not parse_proc_head(astruct,potype_destructor,false,nil,nil,pd);
  1477. if not recover then
  1478. parse_proc_dec_finish(pd,isclassmethod);
  1479. end;
  1480. else
  1481. if (token=_OPERATOR) or
  1482. (isclassmethod and (idtoken=_OPERATOR)) then
  1483. begin
  1484. { we need to set the block type to bt_body, so that operator names
  1485. like ">", "=>" or "<>" are parsed correctly instead of e.g.
  1486. _LSHARPBRACKET and _RSHARPBRACKET for "<>" }
  1487. old_block_type:=block_type;
  1488. block_type:=bt_body;
  1489. consume(_OPERATOR);
  1490. parse_proc_head(astruct,potype_operator,false,nil,nil,pd);
  1491. block_type:=old_block_type;
  1492. if assigned(pd) then
  1493. parse_proc_dec_finish(pd,isclassmethod)
  1494. else
  1495. begin
  1496. { recover }
  1497. try_to_consume(_ID);
  1498. consume(_COLON);
  1499. consume_all_until(_SEMICOLON);
  1500. recover:=true;
  1501. end;
  1502. end;
  1503. end;
  1504. if recover and not(check_proc_directive(false)) then
  1505. begin
  1506. if (token=_COLON) and not(Assigned(pd) and is_void(pd.returndef)) then
  1507. begin
  1508. message(parser_e_field_not_allowed_here);
  1509. consume_all_until(_SEMICOLON);
  1510. end;
  1511. consume(_SEMICOLON);
  1512. end;
  1513. { we've parsed the final semicolon, so stop recording tokens }
  1514. if assigned(pd) and
  1515. (df_generic in pd.defoptions) and
  1516. assigned(pd.genericdecltokenbuf) then
  1517. current_scanner.stoprecordtokens;
  1518. result:=pd;
  1519. end;
  1520. function parse_record_method_dec(astruct: tabstractrecorddef; is_classdef: boolean;hadgeneric:boolean): tprocdef;
  1521. var
  1522. oldparse_only: boolean;
  1523. begin
  1524. oldparse_only:=parse_only;
  1525. parse_only:=true;
  1526. result:=parse_proc_dec(is_classdef,astruct,hadgeneric);
  1527. { this is for error recovery as well as forward }
  1528. { interface mappings, i.e. mapping to a method }
  1529. { which isn't declared yet }
  1530. if assigned(result) then
  1531. begin
  1532. parse_record_proc_directives(result);
  1533. { since records have no inheritance, don't allow non-static
  1534. class methods. Delphi does the same. }
  1535. if (result.proctypeoption<>potype_operator) and
  1536. is_classdef and
  1537. not (po_staticmethod in result.procoptions) then
  1538. MessagePos(result.fileinfo, parser_e_class_methods_only_static_in_records);
  1539. // we can't add hidden params here because record is not yet defined
  1540. // and therefore record size which has influence on paramter passing rules may change too
  1541. // look at record_dec to see where calling conventions are applied (issue #0021044)
  1542. handle_calling_convention(result,[hcc_check]);
  1543. { add definition to procsym }
  1544. proc_add_definition(result);
  1545. end;
  1546. maybe_parse_hint_directives(result);
  1547. parse_only:=oldparse_only;
  1548. end;
  1549. procedure insert_record_hidden_paras(astruct: trecorddef);
  1550. var
  1551. pd: tdef;
  1552. i: longint;
  1553. oldpos : tfileposinfo;
  1554. oldparse_only: boolean;
  1555. begin
  1556. // handle calling conventions of record methods
  1557. oldpos:=current_filepos;
  1558. oldparse_only:=parse_only;
  1559. parse_only:=true;
  1560. { don't keep track of procdefs in a separate list, because the
  1561. compiler may add additional procdefs (e.g. property wrappers for
  1562. the jvm backend) }
  1563. for i := 0 to astruct.symtable.deflist.count - 1 do
  1564. begin
  1565. pd:=tdef(astruct.symtable.deflist[i]);
  1566. if pd.typ<>procdef then
  1567. continue;
  1568. current_filepos:=tprocdef(pd).fileinfo;
  1569. handle_calling_convention(tprocdef(pd),[hcc_insert_hidden_paras]);
  1570. end;
  1571. parse_only:=oldparse_only;
  1572. current_filepos:=oldpos;
  1573. end;
  1574. {****************************************************************************
  1575. Procedure directive handlers
  1576. ****************************************************************************}
  1577. procedure pd_compilerproc(pd:tabstractprocdef);
  1578. var
  1579. v : Tconstexprint;
  1580. begin
  1581. { check for optional syssym index }
  1582. if try_to_consume(_COLON) then
  1583. begin
  1584. v:=get_intconst;
  1585. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1586. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1587. else if not assigned(tsyssym.find_by_number(longint(v.svalue))) then
  1588. message1(parser_e_invalid_internal_function_index,tostr(v))
  1589. else
  1590. tprocdef(pd).extnumber:=longint(v.svalue);
  1591. end;
  1592. end;
  1593. procedure pd_far(pd:tabstractprocdef);
  1594. begin
  1595. pd.declared_far;
  1596. end;
  1597. procedure pd_near(pd:tabstractprocdef);
  1598. begin
  1599. pd.declared_near;
  1600. end;
  1601. procedure pd_export(pd:tabstractprocdef);
  1602. begin
  1603. if pd.typ<>procdef then
  1604. internalerror(200304264);
  1605. if assigned(tprocdef(pd).struct) then
  1606. Message(parser_e_methods_dont_be_export);
  1607. if pd.parast.symtablelevel>normal_function_level then
  1608. Message(parser_e_dont_nest_export);
  1609. end;
  1610. procedure pd_forward(pd:tabstractprocdef);
  1611. begin
  1612. if pd.typ<>procdef then
  1613. internalerror(200304265);
  1614. tprocdef(pd).forwarddef:=true;
  1615. end;
  1616. procedure pd_alias(pd:tabstractprocdef);
  1617. begin
  1618. if pd.typ<>procdef then
  1619. internalerror(200304266);
  1620. consume(_COLON);
  1621. tprocdef(pd).aliasnames.insert(get_stringconst);
  1622. include(pd.procoptions,po_has_public_name);
  1623. end;
  1624. procedure pd_public(pd:tabstractprocdef);
  1625. begin
  1626. if pd.typ<>procdef then
  1627. internalerror(200304266);
  1628. if try_to_consume(_NAME) then
  1629. begin
  1630. tprocdef(pd).aliasnames.insert(get_stringconst);
  1631. include(pd.procoptions,po_has_public_name);
  1632. end;
  1633. end;
  1634. procedure pd_asmname(pd:tabstractprocdef);
  1635. begin
  1636. if pd.typ<>procdef then
  1637. internalerror(200304267);
  1638. if token=_CCHAR then
  1639. begin
  1640. tprocdef(pd).aliasnames.insert(target_info.Cprefix+pattern);
  1641. consume(_CCHAR)
  1642. end
  1643. else
  1644. begin
  1645. tprocdef(pd).aliasnames.insert(target_info.Cprefix+cstringpattern);
  1646. consume(_CSTRING);
  1647. end;
  1648. { we don't need anything else }
  1649. tprocdef(pd).forwarddef:=false;
  1650. end;
  1651. procedure pd_internconst(pd:tabstractprocdef);
  1652. var v:Tconstexprint;
  1653. begin
  1654. if pd.typ<>procdef then
  1655. internalerror(200304268);
  1656. consume(_COLON);
  1657. v:=get_intconst;
  1658. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1659. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1660. else
  1661. Tprocdef(pd).extnumber:=longint(v.svalue);
  1662. end;
  1663. procedure pd_internproc(pd:tabstractprocdef);
  1664. var v:Tconstexprint;
  1665. begin
  1666. if pd.typ<>procdef then
  1667. internalerror(200304268);
  1668. consume(_COLON);
  1669. v:=get_intconst;
  1670. if (v<int64(low(longint))) or (v>int64(high(longint))) then
  1671. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(longint)),tostr(high(longint)))
  1672. else
  1673. Tprocdef(pd).extnumber:=longint(v.svalue);
  1674. { the proc is defined }
  1675. tprocdef(pd).forwarddef:=false;
  1676. end;
  1677. procedure pd_interrupt(pd:tabstractprocdef);
  1678. begin
  1679. if pd.parast.symtablelevel>normal_function_level then
  1680. Message(parser_e_dont_nest_interrupt);
  1681. end;
  1682. procedure pd_abstract(pd:tabstractprocdef);
  1683. begin
  1684. if pd.typ<>procdef then
  1685. internalerror(200304269);
  1686. if is_objectpascal_helper(tprocdef(pd).struct) then
  1687. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_ABSTRACT].str);
  1688. if assigned(tprocdef(pd).struct) and
  1689. (oo_is_sealed in tprocdef(pd).struct.objectoptions) then
  1690. Message(parser_e_sealed_class_cannot_have_abstract_methods)
  1691. else if (po_virtualmethod in pd.procoptions) then
  1692. begin
  1693. include(pd.procoptions,po_abstractmethod);
  1694. { one more abstract method }
  1695. inc(tobjectdef(pd.owner.defowner).abstractcnt);
  1696. end
  1697. else
  1698. Message(parser_e_only_virtual_methods_abstract);
  1699. { the method is defined }
  1700. tprocdef(pd).forwarddef:=false;
  1701. end;
  1702. procedure pd_final(pd:tabstractprocdef);
  1703. begin
  1704. if pd.typ<>procdef then
  1705. internalerror(200910170);
  1706. if is_objectpascal_helper(tprocdef(pd).struct) and
  1707. (m_objfpc in current_settings.modeswitches) then
  1708. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_FINAL].str);
  1709. if (po_virtualmethod in pd.procoptions) or
  1710. (is_javaclass(tprocdef(pd).struct) and
  1711. (po_classmethod in pd.procoptions)) then
  1712. include(pd.procoptions,po_finalmethod)
  1713. else
  1714. Message(parser_e_only_virtual_methods_final);
  1715. end;
  1716. procedure pd_enumerator(pd:tabstractprocdef);
  1717. begin
  1718. if pd.typ<>procdef then
  1719. internalerror(200910250);
  1720. if (token = _ID) then
  1721. begin
  1722. if pattern='MOVENEXT' then
  1723. begin
  1724. if oo_has_enumerator_movenext in tprocdef(pd).struct.objectoptions then
  1725. message(parser_e_only_one_enumerator_movenext);
  1726. pd.calcparas;
  1727. if (pd.proctypeoption = potype_function) and is_boolean(pd.returndef) and
  1728. (pd.minparacount = 0) then
  1729. begin
  1730. include(tprocdef(pd).struct.objectoptions, oo_has_enumerator_movenext);
  1731. include(pd.procoptions,po_enumerator_movenext);
  1732. end
  1733. else
  1734. Message(parser_e_enumerator_movenext_is_not_valid)
  1735. end
  1736. else
  1737. Message1(parser_e_invalid_enumerator_identifier, pattern);
  1738. consume(token);
  1739. end
  1740. else
  1741. Message(parser_e_enumerator_identifier_required);
  1742. end;
  1743. procedure pd_virtual(pd:tabstractprocdef);
  1744. {$ifdef WITHDMT}
  1745. var
  1746. pt : tnode;
  1747. {$endif WITHDMT}
  1748. begin
  1749. if (not assigned(pd.owner.defowner) or
  1750. not is_java_class_or_interface(tdef(pd.owner.defowner))) and
  1751. (po_external in pd.procoptions) then
  1752. Message1(parser_e_proc_dir_conflict,'EXTERNAL');
  1753. if pd.typ<>procdef then
  1754. internalerror(2003042610);
  1755. if (pd.proctypeoption=potype_constructor) and
  1756. is_object(tprocdef(pd).struct) then
  1757. Message(parser_e_constructor_cannot_be_not_virtual);
  1758. if pd.is_generic then
  1759. message(parser_e_genfuncs_cannot_be_virtual);
  1760. if is_objectpascal_helper(tprocdef(pd).struct) and
  1761. (m_objfpc in current_settings.modeswitches) then
  1762. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_VIRTUAL].str);
  1763. {$ifdef WITHDMT}
  1764. if is_object(tprocdef(pd).struct) and
  1765. (token<>_SEMICOLON) then
  1766. begin
  1767. { any type of parameter is allowed here! }
  1768. pt:=comp_expr(true);
  1769. if is_constintnode(pt) then
  1770. begin
  1771. include(pd.procoptions,po_msgint);
  1772. pd.messageinf.i:=pt.value;
  1773. end
  1774. else
  1775. Message(parser_e_ill_msg_expr);
  1776. disposetree(pt);
  1777. end;
  1778. {$endif WITHDMT}
  1779. end;
  1780. procedure pd_dispid(pd:tabstractprocdef);
  1781. var pt:Tnode;
  1782. begin
  1783. if pd.typ<>procdef then
  1784. internalerror(200604301);
  1785. pt:=comp_expr([ef_accept_equal]);
  1786. if is_constintnode(pt) then
  1787. if (Tordconstnode(pt).value<int64(low(longint))) or (Tordconstnode(pt).value>int64(high(longint))) then
  1788. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(longint)),tostr(high(longint)))
  1789. else
  1790. Tprocdef(pd).dispid:=Tordconstnode(pt).value.svalue
  1791. else
  1792. message(parser_e_dispid_must_be_ord_const);
  1793. pt.free;
  1794. end;
  1795. procedure pd_static(pd:tabstractprocdef);
  1796. begin
  1797. if pd.typ<>procdef then
  1798. internalerror(2013032001);
  1799. if not assigned(tprocdef(pd).struct) then
  1800. internalerror(2013032002);
  1801. include(tprocdef(pd).procsym.symoptions,sp_static);
  1802. { "static" is not allowed for operators or normal methods (except in objects) }
  1803. if (pd.proctypeoption=potype_operator) or
  1804. (
  1805. not (po_classmethod in pd.procoptions) and
  1806. not is_object(tprocdef(pd).struct)
  1807. )
  1808. then
  1809. Message1(parser_e_dir_not_allowed,arraytokeninfo[_STATIC].str);
  1810. include(pd.procoptions,po_staticmethod);
  1811. end;
  1812. procedure pd_override(pd:tabstractprocdef);
  1813. begin
  1814. if pd.typ<>procdef then
  1815. internalerror(2003042611);
  1816. if is_objectpascal_helper(tprocdef(pd).struct) then
  1817. begin
  1818. if m_objfpc in current_settings.modeswitches then
  1819. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_OVERRIDE].str)
  1820. end
  1821. else if not(is_class_or_interface_or_objc_or_java(tprocdef(pd).struct)) then
  1822. Message(parser_e_no_object_override)
  1823. else if is_objccategory(tprocdef(pd).struct) then
  1824. Message(parser_e_no_category_override)
  1825. else if (po_external in pd.procoptions) and
  1826. not is_objc_class_or_protocol(tprocdef(pd).struct) and
  1827. not is_cppclass(tprocdef(pd).struct) and
  1828. not is_java_class_or_interface(tprocdef(pd).struct) then
  1829. Message1(parser_e_proc_dir_conflict,'OVERRIDE');
  1830. end;
  1831. procedure pd_overload(pd:tabstractprocdef);
  1832. begin
  1833. if pd.typ<>procdef then
  1834. internalerror(2003042612);
  1835. include(tprocdef(pd).procsym.symoptions,sp_has_overloaded);
  1836. end;
  1837. procedure pd_message(pd:tabstractprocdef);
  1838. var
  1839. pt : tnode;
  1840. paracnt : longint;
  1841. begin
  1842. if pd.typ<>procdef then
  1843. internalerror(2003042613);
  1844. if is_objectpascal_helper(tprocdef(pd).struct) then
  1845. begin
  1846. if m_objfpc in current_settings.modeswitches then
  1847. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_MESSAGE].str);
  1848. end
  1849. else
  1850. if not is_class(tprocdef(pd).struct) and
  1851. not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1852. Message(parser_e_msg_only_for_classes);
  1853. if ([po_msgstr,po_msgint]*pd.procoptions)<>[] then
  1854. Message(parser_e_multiple_messages);
  1855. { check parameter type }
  1856. if not is_objc_class_or_protocol(tprocdef(pd).struct) then
  1857. begin
  1858. if po_external in pd.procoptions then
  1859. Message1(parser_e_proc_dir_conflict,'MESSAGE');
  1860. paracnt:=0;
  1861. pd.parast.SymList.ForEachCall(@check_msg_para,@paracnt);
  1862. if paracnt<>1 then
  1863. Message(parser_e_ill_msg_param);
  1864. end;
  1865. pt:=comp_expr([ef_accept_equal]);
  1866. { message is 1-character long }
  1867. if is_constcharnode(pt) then
  1868. begin
  1869. include(pd.procoptions,po_msgstr);
  1870. tprocdef(pd).messageinf.str:=stringdup(chr(byte(tordconstnode(pt).value.uvalue and $FF)));
  1871. end
  1872. else if pt.nodetype=stringconstn then
  1873. begin
  1874. include(pd.procoptions,po_msgstr);
  1875. if (tstringconstnode(pt).len>255) then
  1876. Message(parser_e_message_string_too_long);
  1877. tprocdef(pd).messageinf.str:=stringdup(tstringconstnode(pt).value_str);
  1878. end
  1879. else
  1880. if is_constintnode(pt) and
  1881. (is_class(tprocdef(pd).struct) or
  1882. is_objectpascal_helper(tprocdef(pd).struct)) then
  1883. begin
  1884. include(pd.procoptions,po_msgint);
  1885. if (Tordconstnode(pt).value<int64(low(Tprocdef(pd).messageinf.i))) or
  1886. (Tordconstnode(pt).value>int64(high(Tprocdef(pd).messageinf.i))) then
  1887. message3(type_e_range_check_error_bounds,tostr(Tordconstnode(pt).value),tostr(low(Tprocdef(pd).messageinf.i)),tostr(high(Tprocdef(pd).messageinf.i)))
  1888. else
  1889. Tprocdef(pd).messageinf.i:=tordconstnode(pt).value.svalue;
  1890. end
  1891. else
  1892. Message(parser_e_ill_msg_expr);
  1893. { check whether the selector name is valid in case of Objective-C }
  1894. if (po_msgstr in pd.procoptions) and
  1895. is_objc_class_or_protocol(tprocdef(pd).struct) and
  1896. not objcvalidselectorname(@tprocdef(pd).messageinf.str^[1],length(tprocdef(pd).messageinf.str^)) then
  1897. Message1(type_e_invalid_objc_selector_name,tprocdef(pd).messageinf.str^);
  1898. pt.free;
  1899. end;
  1900. procedure pd_reintroduce(pd:tabstractprocdef);
  1901. begin
  1902. if pd.typ<>procdef then
  1903. internalerror(200401211);
  1904. if is_objectpascal_helper(tprocdef(pd).struct) then
  1905. begin
  1906. if m_objfpc in current_settings.modeswitches then
  1907. Message1(parser_e_not_allowed_in_helper, arraytokeninfo[_REINTRODUCE].str);
  1908. end
  1909. else
  1910. if not(is_class_or_interface_or_object(tprocdef(pd).struct)) and
  1911. not(is_objccategory(tprocdef(pd).struct)) and
  1912. not(is_javaclass(tprocdef(pd).struct)) then
  1913. Message(parser_e_no_object_reintroduce);
  1914. end;
  1915. procedure pd_syscall(pd:tabstractprocdef);
  1916. procedure include_po_syscall;
  1917. var
  1918. syscall: psyscallinfo;
  1919. begin
  1920. case target_info.system of
  1921. system_m68k_atari,
  1922. system_m68k_amiga,
  1923. system_powerpc_amiga:
  1924. include(pd.procoptions,get_default_syscall);
  1925. system_powerpc_morphos,
  1926. system_arm_aros,
  1927. system_i386_aros,
  1928. system_x86_64_aros:
  1929. begin
  1930. syscall:=get_syscall_by_token(idtoken);
  1931. if assigned(syscall) then
  1932. begin
  1933. if target_info.system in syscall^.validon then
  1934. begin
  1935. consume(idtoken);
  1936. include(pd.procoptions,syscall^.procoption);
  1937. end
  1938. end
  1939. else
  1940. include(pd.procoptions,get_default_syscall);
  1941. end;
  1942. end;
  1943. end;
  1944. function po_syscall_to_varoptions: tvaroptions;
  1945. begin
  1946. result:=[vo_is_syscall_lib,vo_is_hidden_para];
  1947. if ([po_syscall_legacy,po_syscall_basereg,po_syscall_basenone] * tprocdef(pd).procoptions) <> [] then
  1948. include(result,vo_has_explicit_paraloc);
  1949. end;
  1950. function po_syscall_to_regname: string;
  1951. begin
  1952. if po_syscall_legacy in tprocdef(pd).procoptions then
  1953. result:='a6'
  1954. { let nobase on MorphOS store the libbase in r12 as well, because
  1955. we will need the libbase anyway during the call generation }
  1956. else if (po_syscall_basenone in tprocdef(pd).procoptions) and
  1957. (target_info.system = system_powerpc_morphos) then
  1958. result:='r12'
  1959. else if po_syscall_basereg in tprocdef(pd).procoptions then
  1960. begin
  1961. case target_info.system of
  1962. system_i386_aros:
  1963. result:='eax';
  1964. system_x86_64_aros:
  1965. result:='rax';
  1966. system_powerpc_morphos:
  1967. result:='r12';
  1968. else
  1969. internalerror(2016090201);
  1970. end;
  1971. end
  1972. else
  1973. internalerror(2016090101);
  1974. end;
  1975. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  1976. const
  1977. syscall_paranr: array[boolean] of aint =
  1978. ( paranr_syscall_lib_last, paranr_syscall_lib_first );
  1979. var
  1980. vs : tparavarsym;
  1981. sym : tsym;
  1982. symtable : TSymtable;
  1983. v: Tconstexprint;
  1984. vo: tvaroptions;
  1985. paranr: aint;
  1986. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  1987. begin
  1988. if (pd.typ<>procdef) and (target_info.system <> system_powerpc_amiga) then
  1989. internalerror(2003042614);
  1990. tprocdef(pd).forwarddef:=false;
  1991. {$if defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  1992. include_po_syscall;
  1993. if target_info.system = system_m68k_atari then
  1994. begin
  1995. v:=get_intconst;
  1996. if ((v<0) or (v>15)) then
  1997. message(parser_e_range_check_error)
  1998. else
  1999. tprocdef(pd).extnumber:=longint(v.svalue);
  2000. v:=get_intconst;
  2001. if ((v<0) or (v>high(smallint))) then
  2002. message(parser_e_range_check_error)
  2003. else
  2004. tprocdef(pd).import_nr:=longint(v.svalue);
  2005. exit;
  2006. end;
  2007. if consume_sym(sym,symtable) then
  2008. if (sym.typ=staticvarsym) and
  2009. ((tabstractvarsym(sym).vardef.typ=pointerdef) or
  2010. is_32bitint(tabstractvarsym(sym).vardef)) then
  2011. begin
  2012. include(pd.procoptions,po_syscall_has_libsym);
  2013. tcpuprocdef(pd).libsym:=sym;
  2014. vo:=po_syscall_to_varoptions;
  2015. paranr:=syscall_paranr[po_syscall_basefirst in tprocdef(pd).procoptions];
  2016. vs:=cparavarsym.create('$syscalllib',paranr,vs_value,tabstractvarsym(sym).vardef,vo);
  2017. if vo_has_explicit_paraloc in vo then
  2018. if not paramanager.parseparaloc(vs,po_syscall_to_regname) then
  2019. internalerror(2016120301);
  2020. pd.parast.insert(vs);
  2021. end
  2022. else
  2023. Message(parser_e_32bitint_or_pointer_variable_expected);
  2024. paramanager.create_funcretloc_info(pd,calleeside);
  2025. paramanager.create_funcretloc_info(pd,callerside);
  2026. v:=get_intconst;
  2027. if (v<low(Tprocdef(pd).extnumber)) or (v>high(Tprocdef(pd).extnumber)) then
  2028. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(Tprocdef(pd).extnumber)),tostr(high(Tprocdef(pd).extnumber)))
  2029. else
  2030. if target_info.system in [system_arm_aros,system_i386_aros,system_x86_64_aros] then
  2031. Tprocdef(pd).extnumber:=v.uvalue * sizeof(pint)
  2032. else
  2033. Tprocdef(pd).extnumber:=v.uvalue;
  2034. {$endif defined(powerpc) or defined(m68k) or defined(i386) or defined(x86_64) or defined(arm)}
  2035. end;
  2036. procedure pd_external(pd:tabstractprocdef);
  2037. {
  2038. If import_dll=nil the procedure is assumed to be in another
  2039. object file. In that object file it should have the name to
  2040. which import_name is pointing to. Otherwise, the procedure is
  2041. assumed to be in the DLL to which import_dll is pointing to. In
  2042. that case either import_nr<>0 or import_name<>nil is true, so
  2043. the procedure is either imported by number or by name. (DM)
  2044. }
  2045. var
  2046. hs : string;
  2047. v:Tconstexprint;
  2048. is_java_external: boolean;
  2049. begin
  2050. if pd.typ<>procdef then
  2051. internalerror(2003042615);
  2052. { Allow specifying a separate external name for methods in external Java
  2053. because its identifier naming constraints are laxer than FPC's
  2054. (e.g., case sensitive).
  2055. Limitation: only allows specifying the symbol name and not the package name,
  2056. and only for external classes/interfaces }
  2057. is_java_external:=
  2058. (pd.typ=procdef) and
  2059. is_java_class_or_interface(tdef(pd.owner.defowner)) and
  2060. (oo_is_external in tobjectdef(pd.owner.defowner).objectoptions);
  2061. with tprocdef(pd) do
  2062. begin
  2063. forwarddef:=false;
  2064. { forbid local external procedures }
  2065. if parast.symtablelevel>normal_function_level then
  2066. Message(parser_e_no_local_proc_external);
  2067. { If the procedure should be imported from a DLL, a constant string follows.
  2068. This isn't really correct, an contant string expression follows
  2069. so we check if an semicolon follows, else a string constant have to
  2070. follow (FK) }
  2071. if not is_java_external and
  2072. not(token=_SEMICOLON) and not(idtoken=_NAME) then
  2073. begin
  2074. { Always add library prefix and suffix to create an uniform name }
  2075. hs:=get_stringconst;
  2076. if ExtractFileExt(hs)='' then
  2077. hs:=ChangeFileExt(hs,target_info.sharedlibext);
  2078. if Copy(hs,1,length(target_info.sharedlibprefix))<>target_info.sharedlibprefix then
  2079. hs:=target_info.sharedlibprefix+hs;
  2080. { the JVM expects java/lang/Object rather than java.lang.Object }
  2081. if target_info.system in systems_jvm then
  2082. Replace(hs,'.','/');
  2083. import_dll:=stringdup(hs);
  2084. include(procoptions,po_has_importdll);
  2085. if (idtoken=_NAME) then
  2086. begin
  2087. consume(_NAME);
  2088. import_name:=stringdup(get_stringconst);
  2089. include(procoptions,po_has_importname);
  2090. if import_name^='' then
  2091. message(parser_e_empty_import_name);
  2092. end;
  2093. if (idtoken=_INDEX) then
  2094. begin
  2095. {After the word index follows the index number in the DLL.}
  2096. consume(_INDEX);
  2097. v:=get_intconst;
  2098. if (v<int64(low(import_nr))) or (v>int64(high(import_nr))) then
  2099. message(parser_e_range_check_error)
  2100. else
  2101. import_nr:=longint(v.svalue);
  2102. end;
  2103. { default is to used the realname of the procedure }
  2104. if (import_nr=0) and not assigned(import_name) then
  2105. begin
  2106. import_name:=stringdup(procsym.realname);
  2107. include(procoptions,po_has_importname);
  2108. end;
  2109. end
  2110. else
  2111. begin
  2112. if (idtoken=_NAME) or
  2113. is_java_external then
  2114. begin
  2115. consume(_NAME);
  2116. import_name:=stringdup(get_stringconst);
  2117. include(procoptions,po_has_importname);
  2118. if import_name^='' then
  2119. message(parser_e_empty_import_name);
  2120. end;
  2121. end;
  2122. end;
  2123. end;
  2124. procedure pd_weakexternal(pd:tabstractprocdef);
  2125. begin
  2126. if not(target_info.system in systems_weak_linking) then
  2127. message(parser_e_weak_external_not_supported)
  2128. else
  2129. pd_external(pd);
  2130. end;
  2131. procedure pd_winapi(pd:tabstractprocdef);
  2132. begin
  2133. if not(target_info.system in systems_wince) then
  2134. pd.proccalloption:=pocall_cdecl
  2135. else
  2136. pd.proccalloption:=pocall_stdcall;
  2137. end;
  2138. procedure pd_hardfloat(pd:tabstractprocdef);
  2139. begin
  2140. if
  2141. {$if defined(arm)}
  2142. (current_settings.fputype=fpu_soft) or
  2143. {$endif defined(arm)}
  2144. (cs_fp_emulation in current_settings.moduleswitches) then
  2145. message(parser_e_cannot_use_hardfloat_in_a_softfloat_environment);
  2146. end;
  2147. type
  2148. pd_handler=procedure(pd:tabstractprocdef);
  2149. proc_dir_rec=record
  2150. idtok : ttoken;
  2151. pd_flags : tpdflags;
  2152. handler : pd_handler;
  2153. pocall : tproccalloption;
  2154. pooption : tprocoptions;
  2155. mutexclpocall : tproccalloptions;
  2156. mutexclpotype : tproctypeoptions;
  2157. mutexclpo : tprocoptions;
  2158. end;
  2159. const
  2160. {Should contain the number of procedure directives we support.}
  2161. num_proc_directives=46;
  2162. proc_direcdata:array[1..num_proc_directives] of proc_dir_rec=
  2163. (
  2164. (
  2165. idtok:_ABSTRACT;
  2166. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2167. handler : @pd_abstract;
  2168. pocall : pocall_none;
  2169. pooption : [po_abstractmethod];
  2170. mutexclpocall : [pocall_internproc];
  2171. mutexclpotype : [];
  2172. mutexclpo : [po_exports,po_interrupt,po_inline]
  2173. ),(
  2174. idtok:_ALIAS;
  2175. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2176. handler : @pd_alias;
  2177. pocall : pocall_none;
  2178. pooption : [];
  2179. mutexclpocall : [];
  2180. mutexclpotype : [];
  2181. mutexclpo : [po_external,po_inline]
  2182. ),(
  2183. idtok:_ASMNAME;
  2184. pd_flags : [pd_interface,pd_implemen,pd_notobjintf];
  2185. handler : @pd_asmname;
  2186. pocall : pocall_cdecl;
  2187. pooption : [po_external];
  2188. mutexclpocall : [pocall_internproc];
  2189. mutexclpotype : [];
  2190. mutexclpo : [po_external,po_inline]
  2191. ),(
  2192. idtok:_ASSEMBLER;
  2193. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2194. handler : nil;
  2195. pocall : pocall_none;
  2196. pooption : [po_assembler];
  2197. mutexclpocall : [];
  2198. mutexclpotype : [];
  2199. mutexclpo : [po_external]
  2200. ),(
  2201. idtok:_C; {same as cdecl for mode mac}
  2202. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2203. handler : nil;
  2204. pocall : pocall_cdecl;
  2205. pooption : [];
  2206. mutexclpocall : [];
  2207. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2208. mutexclpo : [po_assembler,po_external]
  2209. ),(
  2210. idtok:_CDECL;
  2211. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2212. handler : nil;
  2213. pocall : pocall_cdecl;
  2214. pooption : [];
  2215. mutexclpocall : [];
  2216. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2217. mutexclpo : [po_assembler,po_external]
  2218. ),(
  2219. idtok:_DISPID;
  2220. pd_flags : [pd_dispinterface];
  2221. handler : @pd_dispid;
  2222. pocall : pocall_none;
  2223. pooption : [po_dispid];
  2224. mutexclpocall : [pocall_internproc];
  2225. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2226. mutexclpo : [po_interrupt,po_external,po_inline]
  2227. ),(
  2228. idtok:_DYNAMIC;
  2229. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord];
  2230. handler : @pd_virtual;
  2231. pocall : pocall_none;
  2232. pooption : [po_virtualmethod];
  2233. mutexclpocall : [pocall_internproc];
  2234. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2235. mutexclpo : [po_exports,po_interrupt,po_external,po_overridingmethod,po_inline]
  2236. ),(
  2237. idtok:_EXPORT;
  2238. pd_flags : [pd_body,pd_interface,pd_implemen,pd_notobjintf,pd_notrecord,pd_nothelper];
  2239. handler : @pd_export;
  2240. pocall : pocall_none;
  2241. pooption : [po_exports,po_global];
  2242. mutexclpocall : [pocall_internproc];
  2243. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2244. mutexclpo : [po_external,po_interrupt,po_inline]
  2245. ),(
  2246. idtok:_EXTERNAL;
  2247. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper,pd_javaclass,pd_intfjava];
  2248. handler : @pd_external;
  2249. pocall : pocall_none;
  2250. pooption : [po_external];
  2251. mutexclpocall : [pocall_syscall];
  2252. { allowed for external cpp classes }
  2253. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2254. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2255. ),(
  2256. idtok:_FAR;
  2257. pd_flags : [pd_implemen,pd_body,pd_interface,pd_procvar,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2258. handler : @pd_far;
  2259. pocall : pocall_none;
  2260. pooption : [];
  2261. mutexclpocall : [pocall_internproc];
  2262. mutexclpotype : [];
  2263. mutexclpo : [po_inline]
  2264. ),(
  2265. idtok:_FAR16;
  2266. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar,pd_notobject,pd_notrecord,pd_nothelper];
  2267. handler : nil;
  2268. pocall : pocall_far16;
  2269. pooption : [];
  2270. mutexclpocall : [];
  2271. mutexclpotype : [];
  2272. mutexclpo : [po_external]
  2273. ),(
  2274. idtok:_FINAL;
  2275. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2276. handler : @pd_final;
  2277. pocall : pocall_none;
  2278. pooption : [po_finalmethod];
  2279. mutexclpocall : [pocall_internproc];
  2280. mutexclpotype : [];
  2281. mutexclpo : [po_exports,po_interrupt,po_inline]
  2282. ),(
  2283. idtok:_FORWARD;
  2284. pd_flags : [pd_implemen,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2285. handler : @pd_forward;
  2286. pocall : pocall_none;
  2287. pooption : [];
  2288. mutexclpocall : [pocall_internproc];
  2289. mutexclpotype : [];
  2290. mutexclpo : [po_external,po_inline]
  2291. ),(
  2292. idtok:_OLDFPCCALL;
  2293. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2294. handler : nil;
  2295. pocall : pocall_oldfpccall;
  2296. pooption : [];
  2297. mutexclpocall : [];
  2298. mutexclpotype : [];
  2299. mutexclpo : []
  2300. ),(
  2301. idtok:_INLINE;
  2302. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2303. handler : nil;
  2304. pocall : pocall_none;
  2305. pooption : [po_inline];
  2306. mutexclpocall : [pocall_safecall];
  2307. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2308. mutexclpo : [po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck]
  2309. ),(
  2310. idtok:_INTERNCONST;
  2311. pd_flags : [pd_interface,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2312. handler : @pd_internconst;
  2313. pocall : pocall_none;
  2314. pooption : [po_internconst];
  2315. mutexclpocall : [];
  2316. mutexclpotype : [potype_operator];
  2317. mutexclpo : []
  2318. ),(
  2319. idtok:_INTERNPROC;
  2320. pd_flags : [pd_interface,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2321. handler : @pd_internproc;
  2322. pocall : pocall_internproc;
  2323. pooption : [];
  2324. mutexclpocall : [];
  2325. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2326. mutexclpo : [po_exports,po_external,po_interrupt,po_assembler,po_iocheck,po_virtualmethod]
  2327. ),(
  2328. idtok:_INTERRUPT;
  2329. pd_flags : [pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2330. handler : @pd_interrupt;
  2331. pocall : pocall_oldfpccall;
  2332. pooption : [po_interrupt];
  2333. mutexclpocall : [pocall_internproc,pocall_cdecl,pocall_cppdecl,pocall_stdcall,pocall_mwpascal,
  2334. pocall_pascal,pocall_far16,pocall_oldfpccall];
  2335. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2336. mutexclpo : [po_external,po_inline,po_exports]
  2337. ),(
  2338. idtok:_IOCHECK;
  2339. pd_flags : [pd_implemen,pd_body,pd_notobjintf];
  2340. handler : nil;
  2341. pocall : pocall_none;
  2342. pooption : [po_iocheck];
  2343. mutexclpocall : [pocall_internproc];
  2344. mutexclpotype : [];
  2345. mutexclpo : [po_external]
  2346. ),(
  2347. idtok:_LOCAL;
  2348. pd_flags : [pd_implemen,pd_body];
  2349. handler : nil;
  2350. pocall : pocall_none;
  2351. pooption : [po_kylixlocal];
  2352. mutexclpocall : [pocall_internproc,pocall_far16];
  2353. mutexclpotype : [];
  2354. mutexclpo : [po_external,po_exports]
  2355. ),(
  2356. idtok:_MESSAGE;
  2357. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_objcprot,pd_notrecord];
  2358. handler : @pd_message;
  2359. pocall : pocall_none;
  2360. pooption : []; { can be po_msgstr or po_msgint }
  2361. mutexclpocall : [pocall_internproc];
  2362. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2363. mutexclpo : [po_interrupt,po_inline]
  2364. ),(
  2365. idtok:_MWPASCAL;
  2366. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2367. handler : nil;
  2368. pocall : pocall_mwpascal;
  2369. pooption : [];
  2370. mutexclpocall : [];
  2371. mutexclpotype : [];
  2372. mutexclpo : []
  2373. ),(
  2374. idtok:_NEAR;
  2375. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf,pd_notrecord,pd_nothelper];
  2376. handler : @pd_near;
  2377. pocall : pocall_none;
  2378. pooption : [];
  2379. mutexclpocall : [pocall_internproc];
  2380. mutexclpotype : [];
  2381. mutexclpo : []
  2382. ),(
  2383. idtok:_NORETURN;
  2384. pd_flags : [pd_implemen,pd_interface,pd_body,pd_notobjintf];
  2385. handler : nil;
  2386. pocall : pocall_none;
  2387. pooption : [po_noreturn];
  2388. mutexclpocall : [];
  2389. mutexclpotype : [potype_constructor,potype_destructor,potype_operator,potype_class_constructor,potype_class_destructor];
  2390. mutexclpo : [po_interrupt,po_virtualmethod,po_iocheck]
  2391. ),(
  2392. idtok:_NOSTACKFRAME;
  2393. pd_flags : [pd_implemen,pd_body,pd_procvar,pd_notobjintf];
  2394. handler : nil;
  2395. pocall : pocall_none;
  2396. pooption : [po_nostackframe];
  2397. mutexclpocall : [pocall_internproc];
  2398. mutexclpotype : [];
  2399. mutexclpo : []
  2400. ),(
  2401. idtok:_OVERLOAD;
  2402. pd_flags : [pd_implemen,pd_interface,pd_body,pd_javaclass,pd_intfjava,pd_objcclass,pd_objcprot];
  2403. handler : @pd_overload;
  2404. pocall : pocall_none;
  2405. pooption : [po_overload];
  2406. mutexclpocall : [pocall_internproc];
  2407. mutexclpotype : [];
  2408. mutexclpo : []
  2409. ),(
  2410. idtok:_OVERRIDE;
  2411. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_javaclass,pd_intfjava,pd_notrecord];
  2412. handler : @pd_override;
  2413. pocall : pocall_none;
  2414. pooption : [po_overridingmethod,po_virtualmethod];
  2415. mutexclpocall : [pocall_internproc];
  2416. mutexclpotype : [];
  2417. mutexclpo : [po_exports,po_interrupt,po_virtualmethod,po_inline]
  2418. ),(
  2419. idtok:_PASCAL;
  2420. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2421. handler : nil;
  2422. pocall : pocall_pascal;
  2423. pooption : [];
  2424. mutexclpocall : [];
  2425. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2426. mutexclpo : [po_external]
  2427. ),(
  2428. idtok:_PUBLIC;
  2429. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobject,pd_notobjintf,pd_notrecord,pd_nothelper];
  2430. handler : @pd_public;
  2431. pocall : pocall_none;
  2432. pooption : [po_public,po_global];
  2433. mutexclpocall : [pocall_internproc];
  2434. mutexclpotype : [];
  2435. mutexclpo : [po_external,po_inline]
  2436. ),(
  2437. idtok:_REGISTER;
  2438. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2439. handler : nil;
  2440. pocall : pocall_register;
  2441. pooption : [];
  2442. mutexclpocall : [];
  2443. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2444. mutexclpo : [po_external]
  2445. ),(
  2446. idtok:_REINTRODUCE;
  2447. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_objcclass,pd_notrecord,pd_javaclass];
  2448. handler : @pd_reintroduce;
  2449. pocall : pocall_none;
  2450. pooption : [po_reintroduce];
  2451. mutexclpocall : [pocall_internproc];
  2452. mutexclpotype : [];
  2453. mutexclpo : [po_external,po_interrupt,po_exports,po_overridingmethod,po_inline]
  2454. ),(
  2455. idtok:_SAFECALL;
  2456. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2457. handler : nil;
  2458. pocall : pocall_safecall;
  2459. pooption : [];
  2460. mutexclpocall : [];
  2461. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2462. mutexclpo : [po_external]
  2463. ),(
  2464. idtok:_SOFTFLOAT;
  2465. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2466. handler : nil;
  2467. pocall : pocall_softfloat;
  2468. pooption : [];
  2469. mutexclpocall : [];
  2470. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2471. { it's available with po_external because the libgcc floating point routines on the arm
  2472. uses this calling convention }
  2473. mutexclpo : []
  2474. ),(
  2475. idtok:_STATIC;
  2476. pd_flags : [pd_interface,pd_implemen,pd_body,pd_object,pd_record,pd_javaclass,pd_notobjintf];
  2477. handler : @pd_static;
  2478. pocall : pocall_none;
  2479. pooption : [po_staticmethod];
  2480. mutexclpocall : [pocall_internproc];
  2481. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2482. mutexclpo : [po_interrupt,po_exports]
  2483. ),(
  2484. idtok:_STDCALL;
  2485. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2486. handler : nil;
  2487. pocall : pocall_stdcall;
  2488. pooption : [];
  2489. mutexclpocall : [];
  2490. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2491. mutexclpo : [po_external]
  2492. ),(
  2493. idtok:_SYSCALL;
  2494. { Different kind of syscalls are valid for AOS68k, AOSPPC and MOS. }
  2495. { FIX ME!!! MorphOS/AOS68k pd_flags should be:
  2496. pd_interface, pd_implemen, pd_notobject, pd_notobjintf (KB) }
  2497. pd_flags : [pd_interface,pd_implemen,pd_procvar];
  2498. handler : @pd_syscall;
  2499. pocall : pocall_syscall;
  2500. pooption : [];
  2501. mutexclpocall : [];
  2502. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2503. mutexclpo : [po_external,po_assembler,po_interrupt,po_exports]
  2504. ),(
  2505. idtok:_VIRTUAL;
  2506. pd_flags : [pd_interface,pd_object,pd_notobjintf,pd_notrecord,pd_javaclass];
  2507. handler : @pd_virtual;
  2508. pocall : pocall_none;
  2509. pooption : [po_virtualmethod];
  2510. mutexclpocall : [pocall_internproc];
  2511. mutexclpotype : [potype_class_constructor,potype_class_destructor];
  2512. mutexclpo : [po_interrupt,po_exports,po_overridingmethod,po_inline]
  2513. ),(
  2514. idtok:_CPPDECL;
  2515. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2516. handler : nil;
  2517. pocall : pocall_cppdecl;
  2518. pooption : [];
  2519. mutexclpocall : [];
  2520. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2521. mutexclpo : [po_assembler,po_external,po_virtualmethod]
  2522. ),(
  2523. idtok:_VARARGS;
  2524. pd_flags : [pd_interface,pd_implemen,pd_procvar,pd_objcclass,pd_objcprot];
  2525. handler : nil;
  2526. pocall : pocall_none;
  2527. pooption : [po_varargs];
  2528. mutexclpocall : [pocall_internproc,pocall_register,
  2529. pocall_far16,pocall_oldfpccall,pocall_mwpascal];
  2530. mutexclpotype : [];
  2531. mutexclpo : [po_assembler,po_interrupt,po_inline]
  2532. ),(
  2533. idtok:_COMPILERPROC;
  2534. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2535. handler : @pd_compilerproc;
  2536. pocall : pocall_none;
  2537. pooption : [po_compilerproc];
  2538. mutexclpocall : [];
  2539. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2540. mutexclpo : [po_interrupt]
  2541. ),(
  2542. idtok:_WEAKEXTERNAL;
  2543. pd_flags : [pd_implemen,pd_interface,pd_notobject,pd_notobjintf,pd_cppobject,pd_notrecord,pd_nothelper];
  2544. handler : @pd_weakexternal;
  2545. pocall : pocall_none;
  2546. { mark it both external and weak external, so we don't have to
  2547. adapt all code for external symbols to also check for weak external
  2548. }
  2549. pooption : [po_external,po_weakexternal];
  2550. mutexclpocall : [pocall_internproc,pocall_syscall];
  2551. { allowed for external cpp classes }
  2552. mutexclpotype : [{potype_constructor,potype_destructor}potype_class_constructor,potype_class_destructor];
  2553. mutexclpo : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
  2554. ),(
  2555. idtok:_WINAPI;
  2556. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2557. handler : @pd_winapi;
  2558. pocall : pocall_none;
  2559. pooption : [];
  2560. mutexclpocall : [pocall_stdcall,pocall_cdecl];
  2561. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2562. mutexclpo : [po_external]
  2563. ),(
  2564. idtok:_ENUMERATOR;
  2565. pd_flags : [pd_interface,pd_object,pd_record];
  2566. handler : @pd_enumerator;
  2567. pocall : pocall_none;
  2568. pooption : [];
  2569. mutexclpocall : [pocall_internproc];
  2570. mutexclpotype : [];
  2571. mutexclpo : [po_exports,po_interrupt,po_external,po_inline]
  2572. ),(
  2573. idtok:_RTLPROC;
  2574. pd_flags : [pd_interface,pd_implemen,pd_body,pd_notobjintf];
  2575. handler : nil;
  2576. pocall : pocall_none;
  2577. pooption : [po_rtlproc];
  2578. mutexclpocall : [];
  2579. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2580. mutexclpo : [po_interrupt]
  2581. ),(
  2582. idtok:_HARDFLOAT;
  2583. pd_flags : [pd_interface,pd_implemen,pd_body,pd_procvar];
  2584. handler : @pd_hardfloat;
  2585. pocall : pocall_hardfloat;
  2586. pooption : [];
  2587. mutexclpocall : [];
  2588. mutexclpotype : [potype_constructor,potype_destructor,potype_class_constructor,potype_class_destructor];
  2589. { it's available with po_external because the libgcc floating point routines on the arm
  2590. uses this calling convention }
  2591. mutexclpo : []
  2592. )
  2593. );
  2594. function check_proc_directive(isprocvar:boolean):boolean;
  2595. var
  2596. i : longint;
  2597. begin
  2598. result:=false;
  2599. for i:=1 to num_proc_directives do
  2600. if proc_direcdata[i].idtok=idtoken then
  2601. begin
  2602. if ((not isprocvar) or
  2603. (pd_procvar in proc_direcdata[i].pd_flags)) and
  2604. { don't eat a public directive in classes }
  2605. not((idtoken=_PUBLIC) and (symtablestack.top.symtabletype=ObjectSymtable)) then
  2606. result:=true;
  2607. exit;
  2608. end;
  2609. end;
  2610. function parse_proc_direc(pd:tabstractprocdef;var pdflags:tpdflags):boolean;
  2611. {
  2612. Parse the procedure directive, returns true if a correct directive is found
  2613. }
  2614. var
  2615. p : longint;
  2616. found : boolean;
  2617. name : TIDString;
  2618. begin
  2619. parse_proc_direc:=false;
  2620. name:=tokeninfo^[idtoken].str;
  2621. found:=false;
  2622. { Hint directive? Then exit immediatly }
  2623. if (m_hintdirective in current_settings.modeswitches) then
  2624. begin
  2625. case idtoken of
  2626. _LIBRARY,
  2627. _PLATFORM,
  2628. _UNIMPLEMENTED,
  2629. _EXPERIMENTAL,
  2630. _DEPRECATED :
  2631. if (m_delphi in current_settings.modeswitches) and (pd.typ=procdef) then
  2632. begin
  2633. maybe_parse_hint_directives(tprocdef(pd));
  2634. { could the new token still be a directive? }
  2635. if token<>_ID then
  2636. exit;
  2637. name:=tokeninfo^[idtoken].str;
  2638. end
  2639. else
  2640. exit;
  2641. end;
  2642. end;
  2643. { C directive is MacPas only, because it breaks too much existing code
  2644. on other platforms (PFV) }
  2645. if (idtoken=_C) and
  2646. not(m_mac in current_settings.modeswitches) then
  2647. exit;
  2648. { retrieve data for directive if found }
  2649. for p:=1 to num_proc_directives do
  2650. if proc_direcdata[p].idtok=idtoken then
  2651. begin
  2652. found:=true;
  2653. break;
  2654. end;
  2655. { Check if the procedure directive is known }
  2656. if not found then
  2657. begin
  2658. { parsing a procvar type the name can be any
  2659. next variable !! }
  2660. if ((pdflags * [pd_procvar,pd_object,pd_record,pd_objcclass,pd_objcprot])=[]) and
  2661. not(idtoken in [_PROPERTY,_GENERIC]) then
  2662. Message1(parser_w_unknown_proc_directive_ignored,pattern);
  2663. exit;
  2664. end;
  2665. { check if method and directive not for object, like public.
  2666. This needs to be checked also for procvars }
  2667. if (pd_notobject in proc_direcdata[p].pd_flags) and
  2668. (symtablestack.top.symtabletype=ObjectSymtable) and
  2669. { directive allowed for cpp classes? }
  2670. not((pd_cppobject in proc_direcdata[p].pd_flags) and is_cppclass(tdef(symtablestack.top.defowner))) and
  2671. not((pd_javaclass in proc_direcdata[p].pd_flags) and is_javaclass(tdef(symtablestack.top.defowner))) and
  2672. not((pd_intfjava in proc_direcdata[p].pd_flags) and is_javainterface(tdef(symtablestack.top.defowner))) then
  2673. exit;
  2674. if (pd_notrecord in proc_direcdata[p].pd_flags) and
  2675. (symtablestack.top.symtabletype=recordsymtable) then
  2676. exit;
  2677. { check if method and directive not for java class }
  2678. if not(pd_javaclass in proc_direcdata[p].pd_flags) and
  2679. is_javaclass(tdef(symtablestack.top.defowner)) then
  2680. exit;
  2681. { check if method and directive not for java interface }
  2682. if not(pd_intfjava in proc_direcdata[p].pd_flags) and
  2683. is_javainterface(tdef(symtablestack.top.defowner)) then
  2684. exit;
  2685. { Conflicts between directives ? }
  2686. if (pd.proctypeoption in proc_direcdata[p].mutexclpotype) or
  2687. (pd.proccalloption in proc_direcdata[p].mutexclpocall) or
  2688. ((pd.procoptions*proc_direcdata[p].mutexclpo)<>[]) then
  2689. begin
  2690. Message1(parser_e_proc_dir_conflict,name);
  2691. exit;
  2692. end;
  2693. { set calling convention }
  2694. if proc_direcdata[p].pocall<>pocall_none then
  2695. begin
  2696. if (po_hascallingconvention in pd.procoptions) then
  2697. begin
  2698. Message2(parser_w_proc_overriding_calling,
  2699. proccalloptionStr[pd.proccalloption],
  2700. proccalloptionStr[proc_direcdata[p].pocall]);
  2701. end;
  2702. { check if the target processor supports this calling convention }
  2703. if not(proc_direcdata[p].pocall in supported_calling_conventions) then
  2704. begin
  2705. Message1(parser_e_illegal_calling_convention,proccalloptionStr[proc_direcdata[p].pocall]);
  2706. { recover }
  2707. proc_direcdata[p].pocall:=pocall_stdcall;
  2708. end;
  2709. pd.proccalloption:=proc_direcdata[p].pocall;
  2710. include(pd.procoptions,po_hascallingconvention);
  2711. end;
  2712. if pd.typ=procdef then
  2713. begin
  2714. { Check if the directive is only for objects }
  2715. if (pd_object in proc_direcdata[p].pd_flags) and
  2716. not assigned(tprocdef(pd).struct) then
  2717. exit;
  2718. { Check if the directive is only for records }
  2719. if (pd_record in proc_direcdata[p].pd_flags) and
  2720. not assigned(tprocdef(pd).struct) then
  2721. exit;
  2722. { check if method and directive not for interface }
  2723. if (pd_notobjintf in proc_direcdata[p].pd_flags) and
  2724. is_interface(tprocdef(pd).struct) then
  2725. exit;
  2726. { check if method and directive not for interface }
  2727. if is_dispinterface(tprocdef(pd).struct) and
  2728. not(pd_dispinterface in proc_direcdata[p].pd_flags) then
  2729. exit;
  2730. { check if method and directive not for objcclass }
  2731. if is_objcclass(tprocdef(pd).struct) and
  2732. not(pd_objcclass in proc_direcdata[p].pd_flags) then
  2733. exit;
  2734. { check if method and directive not for objcprotocol }
  2735. if is_objcprotocol(tprocdef(pd).struct) and
  2736. not(pd_objcprot in proc_direcdata[p].pd_flags) then
  2737. exit;
  2738. { check if method and directive not for record/class helper }
  2739. if is_objectpascal_helper(tprocdef(pd).struct) and
  2740. (pd_nothelper in proc_direcdata[p].pd_flags) then
  2741. end;
  2742. { consume directive, and turn flag on }
  2743. consume(token);
  2744. parse_proc_direc:=true;
  2745. { Check the pd_flags if the directive should be allowed }
  2746. if (pd_interface in pdflags) and
  2747. not(pd_interface in proc_direcdata[p].pd_flags) then
  2748. begin
  2749. Message1(parser_e_proc_dir_not_allowed_in_interface,name);
  2750. exit;
  2751. end;
  2752. if (pd_implemen in pdflags) and
  2753. not(pd_implemen in proc_direcdata[p].pd_flags) then
  2754. begin
  2755. Message1(parser_e_proc_dir_not_allowed_in_implementation,name);
  2756. exit;
  2757. end;
  2758. if (pd_procvar in pdflags) and
  2759. not(pd_procvar in proc_direcdata[p].pd_flags) then
  2760. begin
  2761. Message1(parser_e_proc_dir_not_allowed_in_procvar,name);
  2762. exit;
  2763. end;
  2764. { Return the new pd_flags }
  2765. if not(pd_body in proc_direcdata[p].pd_flags) then
  2766. exclude(pdflags,pd_body);
  2767. { Add the correct flag }
  2768. pd.procoptions:=pd.procoptions+proc_direcdata[p].pooption;
  2769. { Call the handler }
  2770. if pointer(proc_direcdata[p].handler)<>nil then
  2771. proc_direcdata[p].handler(pd);
  2772. end;
  2773. function proc_get_importname(pd:tprocdef):string;
  2774. var
  2775. dllname, importname : string;
  2776. begin
  2777. result:='';
  2778. if not(po_external in pd.procoptions) then
  2779. internalerror(200412151);
  2780. { external name or number is specified }
  2781. if assigned(pd.import_name) or (pd.import_nr<>0) then
  2782. begin
  2783. if assigned(pd.import_dll) then
  2784. dllname:=pd.import_dll^
  2785. else
  2786. dllname:='';
  2787. if assigned(pd.import_name) then
  2788. importname:=pd.import_name^
  2789. else
  2790. importname:='';
  2791. proc_get_importname:=make_dllmangledname(dllname,
  2792. importname,pd.import_nr,pd.proccalloption);
  2793. end
  2794. else
  2795. begin
  2796. { Default names when importing variables }
  2797. case pd.proccalloption of
  2798. pocall_cdecl :
  2799. begin
  2800. if assigned(pd.struct) then
  2801. result:=target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname
  2802. else
  2803. result:=target_info.Cprefix+pd.procsym.realname;
  2804. end;
  2805. pocall_cppdecl :
  2806. begin
  2807. result:=target_info.Cprefix+pd.cplusplusmangledname;
  2808. end;
  2809. else
  2810. begin
  2811. {In MacPas a single "external" has the same effect as "external name 'xxx'" }
  2812. { but according to MacPas mode description
  2813. Cprefix should still be used PM }
  2814. if (m_mac in current_settings.modeswitches) then
  2815. result:=target_info.Cprefix+tprocdef(pd).procsym.realname
  2816. else
  2817. result:=pd.procsym.realname;
  2818. end;
  2819. end;
  2820. end;
  2821. end;
  2822. procedure compilerproc_set_symbol_name(pd: tprocdef);
  2823. begin
  2824. pd.procsym.realname:='$'+lower(pd.procsym.name);
  2825. end;
  2826. procedure proc_set_mangledname(pd:tprocdef);
  2827. var
  2828. s : string;
  2829. begin
  2830. { When the mangledname is already set we aren't allowed to change
  2831. it because it can already be used somewhere (PFV) }
  2832. if not(po_has_mangledname in pd.procoptions) then
  2833. begin
  2834. if (po_external in pd.procoptions) then
  2835. begin
  2836. { External Procedures are only allowed to change the mangledname
  2837. in their first declaration }
  2838. if (pd.forwarddef or (not pd.hasforward)) then
  2839. begin
  2840. s:=proc_get_importname(pd);
  2841. if s<>'' then
  2842. begin
  2843. pd.setmangledname(s);
  2844. end;
  2845. { since this is an external declaration, there won't be an
  2846. implementation that needs to match the original symbol
  2847. again -> immediately convert here }
  2848. if po_compilerproc in pd.procoptions then
  2849. compilerproc_set_symbol_name(pd);
  2850. end
  2851. end
  2852. else
  2853. { Normal procedures }
  2854. begin
  2855. if (po_compilerproc in pd.procoptions) then
  2856. begin
  2857. pd.setmangledname(lower(pd.procsym.name));
  2858. end;
  2859. end;
  2860. end;
  2861. { Public/exported alias names }
  2862. if (([po_public,po_exports]*pd.procoptions)<>[]) and
  2863. not(po_has_public_name in pd.procoptions) then
  2864. begin
  2865. case pd.proccalloption of
  2866. pocall_cdecl :
  2867. begin
  2868. if assigned(pd.struct) then
  2869. pd.aliasnames.insert(target_info.Cprefix+pd.struct.objrealname^+'_'+pd.procsym.realname)
  2870. else
  2871. begin
  2872. { Export names are not mangled on Windows and OS/2, see also pexports.pas }
  2873. if (target_info.system in (systems_all_windows+[system_i386_emx, system_i386_os2])) and
  2874. (po_exports in pd.procoptions) then
  2875. pd.aliasnames.insert(pd.procsym.realname)
  2876. else
  2877. pd.aliasnames.insert(target_info.Cprefix+pd.procsym.realname);
  2878. end;
  2879. end;
  2880. pocall_cppdecl :
  2881. begin
  2882. pd.aliasnames.insert(target_info.Cprefix+pd.cplusplusmangledname);
  2883. end;
  2884. end;
  2885. { prevent adding the alias a second time }
  2886. include(pd.procoptions,po_has_public_name);
  2887. end;
  2888. end;
  2889. procedure handle_calling_convention(pd:tabstractprocdef;flags:thccflags=hcc_all);
  2890. begin
  2891. if hcc_check in flags then
  2892. begin
  2893. { set the default calling convention if none provided }
  2894. if (pd.typ=procdef) and
  2895. (is_objc_class_or_protocol(tprocdef(pd).struct) or
  2896. is_cppclass(tprocdef(pd).struct)) then
  2897. begin
  2898. { none of the explicit calling conventions should be allowed }
  2899. if (po_hascallingconvention in pd.procoptions) then
  2900. internalerror(2009032501);
  2901. if is_cppclass(tprocdef(pd).struct) then
  2902. pd.proccalloption:=pocall_cppdecl
  2903. else
  2904. pd.proccalloption:=pocall_cdecl;
  2905. end
  2906. else if not(po_hascallingconvention in pd.procoptions) then
  2907. pd.proccalloption:=current_settings.defproccall
  2908. else
  2909. begin
  2910. if pd.proccalloption=pocall_none then
  2911. internalerror(200309081);
  2912. end;
  2913. { handle proccall specific settings }
  2914. case pd.proccalloption of
  2915. pocall_cdecl,
  2916. pocall_cppdecl :
  2917. begin
  2918. { check C cdecl para types }
  2919. check_c_para(pd);
  2920. end;
  2921. pocall_far16 :
  2922. begin
  2923. { Temporary stub, must be rewritten to support OS/2 far16 }
  2924. Message1(parser_w_proc_directive_ignored,'FAR16');
  2925. end;
  2926. end;
  2927. { Inlining is enabled and supported? }
  2928. if (po_inline in pd.procoptions) and
  2929. not(cs_do_inline in current_settings.localswitches) then
  2930. begin
  2931. { Give an error if inline is not supported by the compiler mode,
  2932. otherwise only give a hint that this procedure will not be inlined }
  2933. if not(m_default_inline in current_settings.modeswitches) then
  2934. Message(parser_e_proc_inline_not_supported)
  2935. else
  2936. Message(parser_h_inlining_disabled);
  2937. exclude(pd.procoptions,po_inline);
  2938. end;
  2939. { For varargs directive also cdecl and external must be defined }
  2940. if (po_varargs in pd.procoptions) then
  2941. begin
  2942. { check first for external in the interface, if available there
  2943. then the cdecl must also be there since there is no implementation
  2944. available to contain it }
  2945. if parse_only then
  2946. begin
  2947. { if external is available, then cdecl must also be available,
  2948. procvars don't need external }
  2949. if not((po_external in pd.procoptions) or
  2950. (pd.typ=procvardef) or
  2951. { for objcclasses this is checked later, because the entire
  2952. class may be external. }
  2953. is_objc_class_or_protocol(tprocdef(pd).struct)) and
  2954. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal,pocall_stdcall])) then
  2955. Message(parser_e_varargs_need_cdecl_and_external);
  2956. end
  2957. else
  2958. begin
  2959. { both must be defined now }
  2960. if not((po_external in pd.procoptions) or
  2961. (pd.typ=procvardef)) or
  2962. not(pd.proccalloption in (cdecl_pocalls + [pocall_mwpascal,pocall_stdcall])) then
  2963. Message(parser_e_varargs_need_cdecl_and_external);
  2964. end;
  2965. end;
  2966. end;
  2967. if hcc_insert_hidden_paras in flags then
  2968. begin
  2969. { insert hidden high parameters }
  2970. pd.parast.SymList.ForEachCall(@insert_hidden_para,pd);
  2971. { insert hidden self parameter }
  2972. insert_self_and_vmt_para(pd);
  2973. { insert funcret parameter if required }
  2974. insert_funcret_para(pd);
  2975. { Make var parameters regable, this must be done after the calling
  2976. convention is set. }
  2977. { this must be done before parentfp is insert, because getting all cases
  2978. where parentfp must be in a memory location isn't catched properly so
  2979. we put parentfp never in a register }
  2980. pd.parast.SymList.ForEachCall(@set_addr_param_regable,pd);
  2981. { insert parentfp parameter if required }
  2982. insert_parentfp_para(pd);
  2983. end;
  2984. { Calculate parameter tlist }
  2985. pd.calcparas;
  2986. end;
  2987. procedure parse_proc_directives(pd:tabstractprocdef;var pdflags:tpdflags);
  2988. {
  2989. Parse the procedure directives. It does not matter if procedure directives
  2990. are written using ;procdir; or ['procdir'] syntax.
  2991. }
  2992. var
  2993. stoprecording,
  2994. res : boolean;
  2995. begin
  2996. if (m_mac in current_settings.modeswitches) and (cs_externally_visible in current_settings.localswitches) then
  2997. begin
  2998. tprocdef(pd).aliasnames.insert(target_info.Cprefix+tprocdef(pd).procsym.realname);
  2999. include(pd.procoptions,po_public);
  3000. include(pd.procoptions,po_has_public_name);
  3001. include(pd.procoptions,po_global);
  3002. end;
  3003. { methods from external class definitions are all external themselves }
  3004. if (pd.typ=procdef) and
  3005. assigned(tprocdef(pd).struct) and
  3006. (tprocdef(pd).struct.typ=objectdef) and
  3007. (oo_is_external in tobjectdef(tprocdef(pd).struct).objectoptions) then
  3008. tprocdef(pd).make_external;
  3009. { Class constructors and destructor are static class methods in real. }
  3010. { There are many places in the compiler where either class or static }
  3011. { method flag changes the behavior. It is simplier to add them to }
  3012. { the class constructors/destructors options than to fix all the }
  3013. { occurencies. (Paul) }
  3014. if pd.proctypeoption in [potype_class_constructor,potype_class_destructor] then
  3015. begin
  3016. include(pd.procoptions,po_classmethod);
  3017. include(pd.procoptions,po_staticmethod);
  3018. end;
  3019. { for a generic routine we also need to record the procedure }
  3020. { directives, but only if we aren't already recording for a }
  3021. { surrounding generic }
  3022. if pd.is_generic and (pd.typ=procdef) and not current_scanner.is_recording_tokens then
  3023. begin
  3024. current_scanner.startrecordtokens(tprocdef(pd).genericdecltokenbuf);
  3025. stoprecording:=true;
  3026. end
  3027. else
  3028. stoprecording:=false;
  3029. while token in [_ID,_LECKKLAMMER] do
  3030. begin
  3031. if try_to_consume(_LECKKLAMMER) then
  3032. begin
  3033. repeat
  3034. parse_proc_direc(pd,pdflags);
  3035. until not try_to_consume(_COMMA);
  3036. consume(_RECKKLAMMER);
  3037. { we always expect at least '[];' }
  3038. res:=true;
  3039. end
  3040. else
  3041. begin
  3042. res:=parse_proc_direc(pd,pdflags);
  3043. end;
  3044. { A procedure directive normally followed by a semicolon, but in
  3045. a const section or reading a type we should stop when _EQ is found,
  3046. because a constant/default value follows }
  3047. if res then
  3048. begin
  3049. if (block_type=bt_const_type) and
  3050. (token=_EQ) then
  3051. break;
  3052. { support procedure proc;stdcall export; }
  3053. if not(check_proc_directive((pd.typ=procvardef))) then
  3054. begin
  3055. { support "record p : procedure stdcall end;" and
  3056. "var p : procedure stdcall = nil;" }
  3057. if (pd_procvar in pdflags) and
  3058. (token in [_END,_RKLAMMER,_EQ]) then
  3059. break
  3060. else
  3061. begin
  3062. if (token=_COLON) then
  3063. begin
  3064. Message(parser_e_field_not_allowed_here);
  3065. consume_all_until(_SEMICOLON);
  3066. end;
  3067. consume(_SEMICOLON)
  3068. end;
  3069. end;
  3070. end
  3071. else
  3072. break;
  3073. end;
  3074. if stoprecording then
  3075. current_scanner.stoprecordtokens;
  3076. { nostackframe requires assembler, but assembler
  3077. may be specified in the implementation part only,
  3078. and in not required if the function is first forward declared
  3079. if it is a procdef that has forwardef set to true
  3080. we postpone the possible error message to the real implementation
  3081. parse_only does not need to be considered as po_nostackframe
  3082. is an implementation only directive }
  3083. if (po_nostackframe in pd.procoptions) and
  3084. not (po_assembler in pd.procoptions) and
  3085. ((pd.typ<>procdef) or not tprocdef(pd).forwarddef) then
  3086. message(parser_e_nostackframe_without_assembler);
  3087. end;
  3088. procedure parse_var_proc_directives(sym:tsym);
  3089. var
  3090. pdflags : tpdflags;
  3091. pd : tabstractprocdef;
  3092. begin
  3093. pdflags:=[pd_procvar];
  3094. pd:=nil;
  3095. case sym.typ of
  3096. fieldvarsym,
  3097. staticvarsym,
  3098. localvarsym,
  3099. paravarsym :
  3100. pd:=tabstractprocdef(tabstractvarsym(sym).vardef);
  3101. typesym :
  3102. pd:=tabstractprocdef(ttypesym(sym).typedef);
  3103. else
  3104. internalerror(2003042617);
  3105. end;
  3106. if pd.typ<>procvardef then
  3107. internalerror(2003042618);
  3108. { names should never be used anyway }
  3109. parse_proc_directives(pd,pdflags);
  3110. end;
  3111. procedure parse_object_proc_directives(pd:tabstractprocdef);
  3112. var
  3113. pdflags : tpdflags;
  3114. begin
  3115. pdflags:=[pd_object];
  3116. parse_proc_directives(pd,pdflags);
  3117. end;
  3118. procedure parse_record_proc_directives(pd:tabstractprocdef);
  3119. var
  3120. pdflags : tpdflags;
  3121. begin
  3122. pdflags:=[pd_record];
  3123. parse_proc_directives(pd,pdflags);
  3124. end;
  3125. function proc_add_definition(var currpd:tprocdef):boolean;
  3126. function equal_generic_procdefs(fwpd,currpd:tprocdef):boolean;
  3127. var
  3128. i : longint;
  3129. fwtype,
  3130. currtype : ttypesym;
  3131. foundretdef : boolean;
  3132. begin
  3133. result:=false;
  3134. if fwpd.genericparas.count<>currpd.genericparas.count then
  3135. exit;
  3136. { comparing generic declarations is a bit more cumbersome as the
  3137. defs of the generic parameter types are not equal, especially if the
  3138. declaration contains constraints; essentially we have two cases:
  3139. - proc declared in interface of unit (or in class/record/object)
  3140. and defined in implementation; here the fwpd might contain
  3141. constraints while currpd must only contain undefineddefs
  3142. - forward declaration in implementation; this case is not supported
  3143. right now }
  3144. foundretdef:=false;
  3145. for i:=0 to fwpd.genericparas.count-1 do
  3146. begin
  3147. fwtype:=ttypesym(fwpd.genericparas[i]);
  3148. currtype:=ttypesym(currpd.genericparas[i]);
  3149. { if the type in the currpd isn't a pure undefineddef, then we can
  3150. stop right there }
  3151. if (currtype.typedef.typ<>undefineddef) or (df_genconstraint in currtype.typedef.defoptions) then
  3152. exit;
  3153. if not foundretdef then
  3154. begin
  3155. { if the returndef is the same as this parameter's def then this
  3156. needs to be the case for both procdefs }
  3157. foundretdef:=fwpd.returndef=fwtype.typedef;
  3158. if foundretdef xor (currpd.returndef=currtype.typedef) then
  3159. exit;
  3160. end;
  3161. end;
  3162. if compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv,cpo_generic])<>te_exact then
  3163. exit;
  3164. if not foundretdef then
  3165. begin
  3166. if tstoreddef(fwpd.returndef).is_specialization and tstoreddef(currpd.returndef).is_specialization then
  3167. { for specializations we're happy with equal defs instead of exactly the same defs }
  3168. result:=equal_defs(fwpd.returndef,currpd.returndef)
  3169. else
  3170. { the returndef isn't a type parameter, so compare as usual }
  3171. result:=compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact;
  3172. end
  3173. else
  3174. result:=true;
  3175. end;
  3176. {
  3177. Add definition aprocdef to the overloaded definitions of aprocsym. If a
  3178. forwarddef is found and reused it returns true
  3179. }
  3180. var
  3181. fwpd : tprocdef;
  3182. currparasym,
  3183. fwparasym : tsym;
  3184. currparacnt,
  3185. fwparacnt,
  3186. curridx,
  3187. fwidx,
  3188. i : longint;
  3189. po_comp : tprocoptions;
  3190. paracompopt: tcompare_paras_options;
  3191. forwardfound : boolean;
  3192. symentry: TSymEntry;
  3193. item : tlinkedlistitem;
  3194. begin
  3195. forwardfound:=false;
  3196. { check overloaded functions if the same function already exists }
  3197. for i:=0 to tprocsym(currpd.procsym).ProcdefList.Count-1 do
  3198. begin
  3199. fwpd:=tprocdef(tprocsym(currpd.procsym).ProcdefList[i]);
  3200. { can happen for internally generated routines }
  3201. if (fwpd=currpd) then
  3202. begin
  3203. result:=true;
  3204. exit;
  3205. end;
  3206. { Skip overloaded definitions that are declared in other units }
  3207. if fwpd.procsym<>currpd.procsym then
  3208. continue;
  3209. { check the parameters, for delphi/tp it is possible to
  3210. leave the parameters away in the implementation (forwarddef=false).
  3211. But for an overload declared function this is not allowed }
  3212. if { check if empty implementation arguments match is allowed }
  3213. (
  3214. not(m_repeat_forward in current_settings.modeswitches) and
  3215. not(currpd.forwarddef) and
  3216. is_bareprocdef(currpd) and
  3217. not(po_overload in fwpd.procoptions)
  3218. ) or
  3219. (
  3220. fwpd.is_generic and
  3221. currpd.is_generic and
  3222. equal_generic_procdefs(fwpd,currpd)
  3223. ) or
  3224. { check arguments, we need to check only the user visible parameters. The hidden parameters
  3225. can be in a different location because of the calling convention, eg. L-R vs. R-L order (PFV)
  3226. don't check default values here, because routines that are the same except for their default
  3227. values should be reported as mismatches (since you can't overload based on different default
  3228. parameter values) }
  3229. (
  3230. (compare_paras(fwpd.paras,currpd.paras,cp_none,[cpo_ignorehidden,cpo_openequalisexact,cpo_ignoreuniv])=te_exact) and
  3231. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)=te_exact)
  3232. ) then
  3233. begin
  3234. { Check if we've found the forwarddef, if found then
  3235. we need to update the forward def with the current
  3236. implementation settings }
  3237. if fwpd.forwarddef then
  3238. begin
  3239. forwardfound:=true;
  3240. if not(m_repeat_forward in current_settings.modeswitches) and
  3241. (fwpd.proccalloption<>currpd.proccalloption) then
  3242. paracompopt:=[cpo_ignorehidden,cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv]
  3243. else
  3244. paracompopt:=[cpo_comparedefaultvalue,cpo_openequalisexact,cpo_ignoreuniv];
  3245. { Check calling convention }
  3246. if (fwpd.proccalloption<>currpd.proccalloption) then
  3247. begin
  3248. { In delphi it is possible to specify the calling
  3249. convention in the interface or implementation if
  3250. there was no convention specified in the other
  3251. part }
  3252. if (m_delphi in current_settings.modeswitches) then
  3253. begin
  3254. if not(po_hascallingconvention in currpd.procoptions) then
  3255. currpd.proccalloption:=fwpd.proccalloption
  3256. else
  3257. if not(po_hascallingconvention in fwpd.procoptions) then
  3258. fwpd.proccalloption:=currpd.proccalloption
  3259. else
  3260. begin
  3261. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  3262. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3263. { restore interface settings }
  3264. currpd.proccalloption:=fwpd.proccalloption;
  3265. end;
  3266. end
  3267. else
  3268. begin
  3269. MessagePos(currpd.fileinfo,parser_e_call_convention_dont_match_forward);
  3270. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3271. { restore interface settings }
  3272. currpd.proccalloption:=fwpd.proccalloption;
  3273. end;
  3274. end;
  3275. { Check static }
  3276. if (po_staticmethod in fwpd.procoptions) then
  3277. begin
  3278. if not (po_staticmethod in currpd.procoptions) then
  3279. begin
  3280. include(currpd.procoptions, po_staticmethod);
  3281. if (po_classmethod in currpd.procoptions) then
  3282. begin
  3283. { remove self from the hidden paras }
  3284. symentry:=currpd.parast.Find('self');
  3285. if symentry<>nil then
  3286. begin
  3287. currpd.parast.Delete(symentry);
  3288. currpd.calcparas;
  3289. end;
  3290. end;
  3291. end;
  3292. end;
  3293. { Check if the procedure type and return type are correct,
  3294. also the parameters must match also with the type and that
  3295. if the implementation has default parameters, the interface
  3296. also has them and that if they both have them, that they
  3297. have the same value }
  3298. if ((m_repeat_forward in current_settings.modeswitches) or
  3299. not is_bareprocdef(currpd)) and
  3300. (
  3301. (
  3302. fwpd.is_generic and
  3303. currpd.is_generic and
  3304. not equal_generic_procdefs(fwpd,currpd)
  3305. ) or
  3306. (
  3307. (
  3308. not fwpd.is_generic or
  3309. not currpd.is_generic
  3310. ) and
  3311. (
  3312. (compare_paras(fwpd.paras,currpd.paras,cp_all,paracompopt)<>te_exact) or
  3313. (compare_defs(fwpd.returndef,currpd.returndef,nothingn)<>te_exact)
  3314. )
  3315. )
  3316. ) then
  3317. begin
  3318. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3319. fwpd.fullprocname(false));
  3320. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3321. break;
  3322. end;
  3323. { Check if both are declared forward }
  3324. if fwpd.forwarddef and currpd.forwarddef then
  3325. begin
  3326. MessagePos1(currpd.fileinfo,parser_e_function_already_declared_public_forward,
  3327. currpd.fullprocname(false));
  3328. end;
  3329. { internconst or internproc only need to be defined once }
  3330. if (fwpd.proccalloption=pocall_internproc) then
  3331. currpd.proccalloption:=fwpd.proccalloption
  3332. else
  3333. if (currpd.proccalloption=pocall_internproc) then
  3334. fwpd.proccalloption:=currpd.proccalloption;
  3335. { Check procedure options, Delphi requires that class is
  3336. repeated in the implementation for class methods }
  3337. if (m_fpc in current_settings.modeswitches) then
  3338. po_comp:=[po_classmethod,po_varargs,po_methodpointer,po_interrupt]
  3339. else
  3340. po_comp:=[po_classmethod,po_methodpointer];
  3341. if ((po_comp * fwpd.procoptions)<>(po_comp * currpd.procoptions)) or
  3342. (fwpd.proctypeoption <> currpd.proctypeoption) or
  3343. { if the implementation version has an "overload" modifier,
  3344. the interface version must also have it (otherwise we can
  3345. get annoying crashes due to interface crc changes) }
  3346. (not(po_overload in fwpd.procoptions) and
  3347. (po_overload in currpd.procoptions)) then
  3348. begin
  3349. MessagePos1(currpd.fileinfo,parser_e_header_dont_match_forward,
  3350. fwpd.fullprocname(false));
  3351. tprocsym(fwpd.procsym).write_parameter_lists(fwpd);
  3352. { This error is non-fatal, we can recover }
  3353. end;
  3354. { Forward declaration is external? }
  3355. if (po_external in fwpd.procoptions) then
  3356. MessagePos(currpd.fileinfo,parser_e_proc_already_external);
  3357. { Check parameters }
  3358. if (m_repeat_forward in current_settings.modeswitches) or
  3359. (currpd.minparacount>0) then
  3360. begin
  3361. { If mangled names are equal then they have the same amount of arguments }
  3362. { We can check the names of the arguments }
  3363. { both symtables are in the same order from left to right }
  3364. curridx:=0;
  3365. fwidx:=0;
  3366. currparacnt:=currpd.parast.SymList.Count;
  3367. fwparacnt:=fwpd.parast.SymList.Count;
  3368. repeat
  3369. { skip default parameter constsyms }
  3370. while (curridx<currparacnt) and
  3371. (tsym(currpd.parast.SymList[curridx]).typ<>paravarsym) do
  3372. inc(curridx);
  3373. while (fwidx<fwparacnt) and
  3374. (tsym(fwpd.parast.SymList[fwidx]).typ<>paravarsym) do
  3375. inc(fwidx);
  3376. { stop when one of the two lists is at the end }
  3377. if (fwidx>=fwparacnt) or (curridx>=currparacnt) then
  3378. break;
  3379. { compare names of parameters, ignore implictly
  3380. renamed parameters }
  3381. currparasym:=tsym(currpd.parast.SymList[curridx]);
  3382. fwparasym:=tsym(fwpd.parast.SymList[fwidx]);
  3383. if not(sp_implicitrename in currparasym.symoptions) and
  3384. not(sp_implicitrename in fwparasym.symoptions) then
  3385. begin
  3386. if (currparasym.name<>fwparasym.name) then
  3387. begin
  3388. MessagePos3(currpd.fileinfo,parser_e_header_different_var_names,
  3389. tprocsym(currpd.procsym).realname,fwparasym.realname,currparasym.realname);
  3390. break;
  3391. end;
  3392. end;
  3393. { next parameter }
  3394. inc(curridx);
  3395. inc(fwidx);
  3396. until false;
  3397. end;
  3398. { Everything is checked, now we can update the forward declaration
  3399. with the new data from the implementation }
  3400. fwpd.forwarddef:=currpd.forwarddef;
  3401. fwpd.hasforward:=true;
  3402. fwpd.procoptions:=fwpd.procoptions+currpd.procoptions;
  3403. { marked as local but exported from unit? }
  3404. if (po_kylixlocal in fwpd.procoptions) and (fwpd.owner.symtabletype=globalsymtable) then
  3405. MessagePos(fwpd.fileinfo,type_e_cant_export_local);
  3406. if fwpd.extnumber=$ffff then
  3407. fwpd.extnumber:=currpd.extnumber;
  3408. while not currpd.aliasnames.empty do
  3409. fwpd.aliasnames.insert(currpd.aliasnames.getfirst);
  3410. { update fileinfo so position references the implementation,
  3411. also update funcretsym if it is already generated }
  3412. fwpd.fileinfo:=currpd.fileinfo;
  3413. if assigned(fwpd.funcretsym) then
  3414. fwpd.funcretsym.fileinfo:=currpd.fileinfo;
  3415. if assigned(currpd.deprecatedmsg) then
  3416. begin
  3417. stringdispose(fwpd.deprecatedmsg);
  3418. fwpd.deprecatedmsg:=stringdup(currpd.deprecatedmsg^);
  3419. end;
  3420. { import names }
  3421. if assigned(currpd.import_dll) then
  3422. begin
  3423. stringdispose(fwpd.import_dll);
  3424. fwpd.import_dll:=stringdup(currpd.import_dll^);
  3425. end;
  3426. if assigned(currpd.import_name) then
  3427. begin
  3428. stringdispose(fwpd.import_name);
  3429. fwpd.import_name:=stringdup(currpd.import_name^);
  3430. end;
  3431. fwpd.import_nr:=currpd.import_nr;
  3432. { for compilerproc defines we need to rename and update the
  3433. symbolname to lowercase so users can' access it (can't do
  3434. it immediately, because then the implementation symbol
  3435. won't be matched) }
  3436. if po_compilerproc in fwpd.procoptions then
  3437. begin
  3438. compilerproc_set_symbol_name(fwpd);
  3439. current_module.add_public_asmsym(fwpd.procsym.realname,AB_GLOBAL,AT_FUNCTION);
  3440. end;
  3441. if po_public in fwpd.procoptions then
  3442. begin
  3443. item:=fwpd.aliasnames.first;
  3444. while assigned(item) do
  3445. begin
  3446. current_module.add_public_asmsym(TCmdStrListItem(item).str,AB_GLOBAL,AT_FUNCTION);
  3447. item:=item.next;
  3448. end;
  3449. end;
  3450. { Release current procdef }
  3451. currpd.owner.deletedef(currpd);
  3452. currpd:=fwpd;
  3453. end
  3454. else
  3455. begin
  3456. { abstract methods aren't forward defined, but this }
  3457. { needs another error message }
  3458. if (po_abstractmethod in fwpd.procoptions) then
  3459. MessagePos(currpd.fileinfo,parser_e_abstract_no_definition)
  3460. else
  3461. begin
  3462. MessagePos(currpd.fileinfo,parser_e_overloaded_have_same_parameters);
  3463. tprocsym(currpd.procsym).write_parameter_lists(currpd);
  3464. end;
  3465. end;
  3466. { we found one proc with the same arguments, there are no others
  3467. so we can stop }
  3468. break;
  3469. end;
  3470. { check for allowing overload directive }
  3471. if not(m_fpc in current_settings.modeswitches) then
  3472. begin
  3473. { overload directive turns on overloading }
  3474. if ((po_overload in currpd.procoptions) or
  3475. (po_overload in fwpd.procoptions)) then
  3476. begin
  3477. { check if all procs have overloading, but not if the proc is a method or
  3478. already declared forward, then the check is already done }
  3479. if not(fwpd.hasforward or
  3480. assigned(currpd.struct) or
  3481. (currpd.forwarddef<>fwpd.forwarddef) or
  3482. ((po_overload in currpd.procoptions) and
  3483. (po_overload in fwpd.procoptions))) then
  3484. begin
  3485. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3486. break;
  3487. end
  3488. end
  3489. else
  3490. begin
  3491. if not(fwpd.forwarddef) then
  3492. begin
  3493. if (m_tp7 in current_settings.modeswitches) then
  3494. MessagePos(currpd.fileinfo,parser_e_procedure_overloading_is_off)
  3495. else
  3496. MessagePos1(currpd.fileinfo,parser_e_no_overload_for_all_procs,currpd.procsym.realname);
  3497. break;
  3498. end;
  3499. end;
  3500. end; { equal arguments }
  3501. end;
  3502. { if we didn't reuse a forwarddef then we add the procdef to the overloaded
  3503. list }
  3504. if not forwardfound then
  3505. begin
  3506. { can happen in Delphi mode }
  3507. if (currpd.proctypeoption = potype_function) and
  3508. is_void(currpd.returndef) then
  3509. MessagePos1(currpd.fileinfo,parser_e_no_funcret_specified,currpd.procsym.realname);
  3510. tprocsym(currpd.procsym).ProcdefList.Add(currpd);
  3511. if not currpd.forwarddef and (po_public in currpd.procoptions) then
  3512. begin
  3513. item:=currpd.aliasnames.first;
  3514. while assigned(item) do
  3515. begin
  3516. current_module.add_public_asmsym(TCmdStrListItem(item).str,AB_GLOBAL,AT_FUNCTION);
  3517. item:=item.next;
  3518. end;
  3519. end;
  3520. end;
  3521. proc_add_definition:=forwardfound;
  3522. end;
  3523. end.