pdecsub.pas 126 KB

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