| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // Examine.cc -- Helpers for examining AST code.
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Andrew Beach
 | 
|---|
| 10 | // Created On       : Wed Sept 2 14:02 2020
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Fri Dec 10 10:27 2021
 | 
|---|
| 13 | // Update Count     : 1
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "Common/Examine.h"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "AST/Type.hpp"
 | 
|---|
| 19 | #include "CodeGen/OperatorTable.h"
 | 
|---|
| 20 | #include "InitTweak/InitTweak.h"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | DeclarationWithType * isMainFor( FunctionDecl * func, AggregateDecl::Aggregate kind ) {
 | 
|---|
| 23 |         if (func->name != "main") return nullptr;
 | 
|---|
| 24 |         if (func->type->parameters.size() != 1) return nullptr;
 | 
|---|
| 25 | 
 | 
|---|
| 26 |         auto param = func->type->parameters.front();
 | 
|---|
| 27 | 
 | 
|---|
| 28 |         auto type = dynamic_cast<ReferenceType * >(param->get_type());
 | 
|---|
| 29 |         if (!type) return nullptr;
 | 
|---|
| 30 | 
 | 
|---|
| 31 |         auto obj = dynamic_cast<StructInstType *>(type->base);
 | 
|---|
| 32 |         if (!obj) return nullptr;
 | 
|---|
| 33 | 
 | 
|---|
| 34 |         if (kind != obj->baseStruct->kind) return nullptr;
 | 
|---|
| 35 | 
 | 
|---|
| 36 |         return param;
 | 
|---|
| 37 | }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | namespace {
 | 
|---|
| 40 | 
 | 
|---|
| 41 | // getTypeofThis but does some extra checks used in this module.
 | 
|---|
| 42 | const ast::Type * getTypeofThisSolo( const ast::FunctionDecl * func ) {
 | 
|---|
| 43 |         if ( 1 != func->params.size() ) {
 | 
|---|
| 44 |                 return nullptr;
 | 
|---|
| 45 |         }
 | 
|---|
| 46 |         auto ref = func->type->params.front().as<ast::ReferenceType>();
 | 
|---|
| 47 |         return (ref) ? ref->base : nullptr;
 | 
|---|
| 48 | }
 | 
|---|
| 49 | 
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | const ast::DeclWithType * isMainFor(
 | 
|---|
| 53 |                 const ast::FunctionDecl * func, ast::AggregateDecl::Aggregate kind ) {
 | 
|---|
| 54 |         if ( "main" != func->name ) return nullptr;
 | 
|---|
| 55 |         if ( 1 != func->params.size() ) return nullptr;
 | 
|---|
| 56 | 
 | 
|---|
| 57 |         auto param = func->params.front();
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         auto type = dynamic_cast<const ast::ReferenceType *>( param->get_type() );
 | 
|---|
| 60 |         if ( !type ) return nullptr;
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         auto obj = type->base.as<ast::StructInstType>();
 | 
|---|
| 63 |         if ( !obj ) return nullptr;
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         if ( kind != obj->base->kind ) return nullptr;
 | 
|---|
| 66 | 
 | 
|---|
| 67 |         return param;
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | namespace {
 | 
|---|
| 71 |         Type * getDestructorParam( FunctionDecl * func ) {
 | 
|---|
| 72 |                 if ( !CodeGen::isDestructor( func->name ) ) return nullptr;
 | 
|---|
| 73 | 
 | 
|---|
| 74 |                 auto params = func->type->parameters;
 | 
|---|
| 75 |                 if ( 1 != params.size() ) return nullptr;
 | 
|---|
| 76 | 
 | 
|---|
| 77 |                 auto ref = dynamic_cast<ReferenceType *>( params.front()->get_type() );
 | 
|---|
| 78 |                 if ( ref ) {
 | 
|---|
| 79 |                         return ref->base;
 | 
|---|
| 80 |                 }
 | 
|---|
| 81 |                 return nullptr;
 | 
|---|
| 82 |         }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | const ast::Type * getDestructorParam( const ast::FunctionDecl * func ) {
 | 
|---|
| 85 |         if ( !CodeGen::isDestructor( func->name ) ) return nullptr;
 | 
|---|
| 86 |         //return InitTweak::getParamThis( func )->type;
 | 
|---|
| 87 |         return getTypeofThisSolo( func );
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | bool isDestructorFor( FunctionDecl * func, StructDecl * type_decl ) {
 | 
|---|
| 93 |         if ( Type * type = getDestructorParam( func ) ) {
 | 
|---|
| 94 |                 auto stype = dynamic_cast<StructInstType *>( type );
 | 
|---|
| 95 |                 return stype && stype->baseStruct == type_decl;
 | 
|---|
| 96 |         }
 | 
|---|
| 97 |         return false;
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | bool isDestructorFor(
 | 
|---|
| 101 |                 const ast::FunctionDecl * func, const ast::StructDecl * type_decl ) {
 | 
|---|
| 102 |         if ( const ast::Type * type = getDestructorParam( func ) ) {
 | 
|---|
| 103 |                 auto stype = dynamic_cast<const ast::StructInstType *>( type );
 | 
|---|
| 104 |                 return stype && stype->base.get() == type_decl;
 | 
|---|
| 105 |         }
 | 
|---|
| 106 |         return false;
 | 
|---|
| 107 | }
 | 
|---|