pdecsub.pas 129 KB

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